Vue引入axios和element-UI
引入axios
- 输入命令
引入依赖npm install axios
- 再输入命令
npm install --save axios vue-axios
- 配置模板
import Vue from 'vue' import axios from 'axios'
- 在脚手架里面的用法
Vue.axios.get(api).then((response) => { console.log(response.data) }) this.axios.get(api).then((response) => { console.log(response.data)`在这里插入代码片` }) this.$http.get(api).then((response) => { console.log(response.data) })
引入Element-UI
- 输入命令
引入依赖npm install --save element-ui
最后
贴一下我的main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import 'es6-promise/auto';
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios';
Vue.use(ElementUI);
Vue.prototype.$http = axios;
// 拦截响应response,并做一些错误处理
axios.interceptors.response.use((response) => {
const data = response.data;
return data;
}, function (err) {
// console.log(err.response.status);
if (err.response.status == 404) {
router.replace({
path: "/login"
});
}
});
Vue.config.productionTip = false;
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
需要执行的命令:
npm install axios
npm install --save axios vue-axios
npm install --save element-ui
npm install es6-promise --save