vue-项目模块化中this 指向问题
在VUE项目中,我们想把一些主要的代码抽到一个模块中
export default {
  /**
   * 获取本地的群列表数据
   * @param {*} params
   */
  getGroupList () {
    // 这里的this指向的是当前的对象,我们一般想要的是VUE实例的this
  }
}
有两种方法,
方法一, 在调用这个方法的时候把this传进来,但是假如这个模块有很多个方法,每调一次都要传this过来麻烦
方法二: 在 main.js中把VUEX实现导出了,然后在当前模块引用
main.js
let vue = new Vue({
  components: { App },
  router,
  store,
  i18n,
  template: '<App/>'
}).$mount('#app')
export default vue
当前摸块引用
/**
 * 群聊相关的增,删,改,查
 */
import _this from '../../main.js'
export default {
  /**
   * 获取本地的群列表数据
   * @param {*} params
   */
  getGroupList () {
    const getList = _this.$db.models.User
    getList.getUser({
      userId: _this.$store.getters.userId
    }).then(res => {
      console.log(res)
    })
  }
}
解决问题。
                    
                
                
            
        
浙公网安备 33010602011771号