首页 > 开发 > NodeJS > 正文

js文件调用vue中方法报错:属性未定义

2017-09-08 17:11:02  来源:网友分享

1.方法的引入:

import 'vue2-toast/lib/toast.css';import Toast from 'vue2-toast';Vue.use(Toast);

vue中使用,没有问题:

 mounted(){      this.$toast.center('center');    }

自己写了一个Util.js文件,调用该方法报错

 e1.onclick=function () {    this.$toast.center('center');  }

报错截图:

解决方案

 e1.onclick=function () {    this.$toast.center('center');  }

this指向变了 用箭头函数或者外部保存this