1、commonfun.js:
export default{
va:{
'000': "错误0",
'001': "错误1",
'002': "错误2'
},
func:(retcode)=>{
console.log(this);
return this.va[retcode];
}
}
2、main.js:
import vue相关依赖
import commonfun from commonfun
Vue.prototype.$utils=commonfun;
3、应用中的一个组件,login.vue:
export default{
...
methods:{
login(){
this.$utils.func('001')
}
}
}
当login调用func时,执行到return时出错,cannot read property '001'of undefined;打印出的this对象为什么都嵌套在一个a属性中,未知原因,如下:
{
a:{
va: Object,
func: function..
}
}
修改commonfun.js的func函数return this.a.va[retcode]可正常返回;不知a的出处?
_self
就是this
,为什么不是同一个。而箭头函数的this
为上一级的this
,至于是不是a
就不知道了,你这只写了export
也不知道你在哪用的。