uni-app qq小程序分包后,在分包内无法使用 main.js 中 Vue.prototype.$store 的对象

uni-app qq小程序分包后,在分包内无法使用 main.js 中 Vue.prototype.$store 的对象

6 回复

什么分包模式?普通还是独立?建议使用globalData去做全局属性

更多关于uni-app qq小程序分包后,在分包内无法使用 main.js 中 Vue.prototype.$store 的对象的实战教程也可以访问 https://www.itying.com/category-93-b0.html


uniapp自带的普通分包。微信小程序分包都没有这种问题

回复 f***@163.com: uniapp的分包和微信的分包是一样的。有可能分包和主包中的vue实例不一致了,暂时先使用其他方式吧,微信分包你是怎么用的,现在还可以怎么使用

回复 DCloud_UNI_LXH: 谢谢

回复 f***@163.com:第二天电脑开机后就好了,莫名其妙。。。。

在uni-app中,QQ小程序分包后无法访问主包的Vue.prototype.$store,这是因为小程序分包有独立的作用域,主包中挂载的全局变量默认无法在分包中直接使用。

解决方案:

  1. 使用Vuex的模块化引入
    在分包页面中,直接引入Vuex store实例:

    import store from '@/store'
    export default {
      computed: {
        count() {
          return store.state.count
        }
      }
    }
    
  2. 通过getApp()跨包访问
    在主包main.js中挂载到全局app对象:

    // main.js
    import store from '@/store'
    Vue.prototype.$store = store
    App({
      globalData: { store }
    })
    

    分包页面中通过getApp()获取:

    const app = getApp()
    console.log(app.globalData.store.state.xxx)
回到顶部