代码中打印出的_self,this对象为什么都嵌套在一个a属性中????

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的出处?

阅读 1.6k
1 个回答

_self就是this,为什么不是同一个。而箭头函数的this为上一级的this,至于是不是a就不知道了,你这只写了export也不知道你在哪用的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题