• 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)
        })
      }
    
    }
    
    

    解决问题。

  • 相关阅读:
    python函数及模块
    Python分支结构及循环结构
    python基本的知识
    11.21学习总结
    进度日报28
    进度日报27
    进度日报26
    进度日报25
    进度日报24
    11.14学习总结
  • 原文地址:https://www.cnblogs.com/ybixian/p/10706282.html
Copyright © 2020-2023  润新知