如何在vue现目中创建的js文件使用element-ui组件
个人需求
import axios from "axios"
const serve=axios.create({
//配置默认地址
baseURL:"https://www.liulongbin.top:8888/api/private/v1",
timeout:2000,//求情延迟时间
});
//请求拦截
serve.interceptors.request.use(function (config){
return config;
},function(error){
return Promise.reject(error);
}
)
// 响应拦截
serve.interceptors.response.use(function(response){
const {data:res,meta:{status,msg}}=response.data
switch (status) {
case 200:
localStorage.setItem('token',res.token)
this.$notify({
title: "成功",
message: msg,
type: "success",
position: "top-left",
duration: 1000,
showClose:false
});
break;
default:
break;
}
return response
},function(error){
return Promise.reject(error)
})
export default serve
浏览器报错
Uncaught (in promise) TypeError: Cannot read property ‘$notify’ of undefined
如何解决
一,再创建vue实例
import Vue from "vue"
const vue =new Vue()
vue.$notify({
title: "成功",
message: msg,
type: "success",
position: "top-left",
duration: 1000,
showClose:false
});
可以理解为,将vue中挂在的element拿来
因为我再vue中min.js中全局挂载了element给了vue实例
这里我们把它原先的this换成vue实例调用element就好了
二,按需引入element组件
import { Notification } from 'element-ui';
Notification({
title: "成功",
message: msg,
type: "success",
position: "top-left",
duration: 1000,
showClose:false
});