Vue在单独引入js文件中使用ElementUI的组件

问题场景:

我想在vue中的js文件中使用elementUI中的组件,因为我在main.js中引入了element包和它的css,并挂载到了全局vue实例中。然后我就在js文件中这样使用

this.$notify({
title: "失败",
message: "请先登录老铁~",
type: "error"
});

结果报错了,报错信息如下

TypeError: _this.$notify is not a function
at index.js:44

2019-03-26更新

import axios from 'axios'
// 注意:在js文件中使用element组件 按照下面更优雅哦~~
import {
Message,
MessageBox
} from 'element-ui'
import store from '../store'
import {
getToken
} from '@/utils/auth' // 创建axios实例
const service = axios.create({
baseURL: process.env.BASE_API, // api 的 base_url
timeout: 5000 // 请求超时时间
}) // request拦截器
service.interceptors.request.use(
config => {
if (store.getters.token) {
config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
}
return config
},
error => {
// Do something with request error
console.log(error) // for debug
Promise.reject(error)
}
) // response 拦截器
service.interceptors.response.use(
response => {
/**
* code为非20000是抛错 可结合自己业务进行修改
*/
const res = response.data
if (res.code !== 20000) {
Message({
message: res.message,
type: 'error',
duration: 5 * 1000
}) // 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了;
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
MessageBox.confirm(
'你已被登出,可以取消继续留在该页面,或者重新登录',
'确定登出', {
confirmButtonText: '重新登录',
cancelButtonText: '取消',
type: 'warning'
}
).then(() => {
store.dispatch('FedLogOut').then(() => {
location.reload() // 为了重新实例化vue-router对象 避免bug
})
})
}
return Promise.reject('error')
} else {
return response.data
}
},
error => {
console.log('err' + error) // for debug
Message({
message: error.message,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error)
}
) export default service


解决:

在该js文件中

//1.引入vue
import Vue from 'vue';
//2.新创建一个vue实例
let v = new Vue();
//3.在新的实例上使用组件
v.$notify.error({
title: '错误',
message: '未登录 或者token过期'
});

附上我的全局拦截器

axios/index.js

//引入vue
import Vue from 'vue';
import axios from 'axios';
//新创建一个vue实例
let v = new Vue();
//全局状态控制引入
import store from '../store/store'; import * as types from '../store/mutation-types';
import router from '../router' // http request 拦截器
axios.interceptors.request.use(
config => {
if (store.state.userInfo.token) { // 判断是否存在token,如果存在的话,则每个http header都加上token
config.headers.Authorization = `JWT ${store.state.userInfo.token}`;
}
// config.headers.imooc = `5429026`;
return config;
},
err => {
return Promise.reject(err);
}); // http response 拦截器
axios.interceptors.response.use(
undefined,
error => {
let res = error.response;
switch (res.status) {
case 401:
// 返回 401 清除token信息并跳转到登录页面
// store.commit(types.LOGOUT);
//router.replace({
// path: '/app/login',
// query: {redirect: router.currentRoute.fullPath}
//})
console.log('未登录 或者token过期');
this.$notify({
title: "失败",
message: "请先登录老铁~",
type: "error"
});
case 403:
console.log('您没有该操作权限');
// alert('您没有该操作权限');
case 500:
console.log('服务器错误');
// alert('服务器错误');
}
return Promise.reject(error.response.data) // 返回接口返回的错误信息
});

Vue在单独引入js文件中使用ElementUI的组件的更多相关文章

  1. ASP.NET MVC 中单独的JS文件中获取Controller中设定的值

    1,在Controller中的Action 中将指定值写上.       //       // GET: /Home/       public ActionResult Index()       ...

  2. window.location.href 放置在单独的JS文件中使用时问题

    场景:假设当前浏览器地址栏的地址是:http://localhost:8888/SSHBoot/tourist/homeMainAction_signInUI.do, 现在我想在点击按钮时定位到“ht ...

  3. nuxtjs如何在单独的js文件中引入store和router

    nuxtjs里面集成vuex的创建方式改变了,并且官方不建议以导出Vuex实例的方式创建store,并且会在nuxt3里面删除.这样就会存在一个问题,我怎么像普通vue spa项目一样直接 impor ...

  4. 使用Babel将单独的js文件 中的 ES6转码为ES5

      如果你并没有接触过ES6,当你看到下面的代码时,肯定是有点懵逼的(这是什么鬼?心中一万头神兽奔腾而过),但是你没看错,这就是ES6.不管你看不看它,它都在这里. 1 2 3 4 5 6 7 8 9 ...

  5. js文件中使用el表达式问题

    作者:Sang 单独js文件不能用el表达式. 首先,JSP是由服务端执行的,EL表达式自然也由服务端解析执行,因此如果EL所在的脚本在JSP页面内,它是可以获取到值的,这个值在服务器端返回到浏览器端 ...

  6. js文件中函数前加分号和感叹号是什么意思?

    本文转自:http://blog.csdn.net/h_o_w_e/article/details/51388500 !function(){}();   !有什么用? 从语法上来开,JavaScri ...

  7. 如何在js文件中实现获取request.getCotextPath();

    我们在jsp中可以方便的使用“request.getCotext()”来获取工程的根目录. 但是如果我们的js代码存在一个单独的js文件中,这时候再想获取根目录,我们就要自己截取了.可以采用下面的方式 ...

  8. JS文件中的中文在网页引用时显示乱码的简单解决方式

    今天把一个jquery方法从前台cshtml文件转移到单独的js文件中后执行不成功,调试发现if判断中的中文字符串变成了乱码,之前在前台文件中是可以正常显示的,所以判定可能是跟文件的编码方式有关系. ...

  9. vue脚手架使用swiper /引入js文件/引入css文件

    1.安装vue-cli 参考地址:https://github.com/vuejs/vue-cli 如果不使用严格语法需要在后三项打no:(加了挺头疼的,老是报错,但是对自己的代码规范性也是有很大的帮 ...

随机推荐

  1. (转)typedef和#define的用法与区别

    typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程的一部分,但它并不实际分配内存空间,实例像: ...

  2. 算法Sedgewick第四版-第1章基础-024-M/M/1 queue

    /****************************************************************************** * Compilation: javac ...

  3. PAT甲 1095 解码PAT准考证/1153 Decode Registration Card of PAT(优化技巧)

    1095 解码PAT准考证/1153 Decode Registration Card of PAT(25 分) PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级: ...

  4. [转]Passing Managed Structures With Strings To Unmanaged Code Part 2

    1. Introduction. 1.1 In part 1 of this series of blogs we studied how to pass a managed structure (w ...

  5. JavsScript中JSON相关

    1.JSON.parse(jsonString) JSON.parse(jsonString):将一个JSON格式的字符串字面值,转换成JSON对象,它的逆运算方法是JSON.stringify(ob ...

  6. SQL 分组后拼接字符串

    with t as( select 'Charles' parent, 'William' child union select 'Charles', 'Harry' union select 'An ...

  7. 在FooterTemplate内显示DropDownList控件

    如果想在Gridview控件FooterTemplate内显示DropDownList控件供用户添加数据时所应用.有两种方法可以实现,一种是在GridView控件的OnRowDataBound事件中写 ...

  8. 四步走查智能硬件异常Case

    此文已由作者于真真授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 相比于软件,智能硬件产品由于涉及硬件和软件两个端的状态,其异常case要更加错综复杂.由于硬件产品的迭代更新 ...

  9. LinkExtractor 构造器各参数说明

    LinkExtractor 构造器各参数说明 特例: LinkExtractor构造器的所有参数都有默认值 各参数说明: allow 接收一个正则表达式或一个正则表达式列表,提取绝对url与正则表达式 ...

  10. Python列表知识补充

    1.import this  Python之禅,圣经. >>> import this The Zen of Python, by Tim Peters Beautiful is b ...