weex 引入vuex 报错

问题: weex 引入vuex 卡在了这个上

2018-12-11 14:59:54.696233+0800 XYT[13100:4041493] <Weex>[log]WXJSCoreBridge.m:145, jsLog: 111
2018-12-11 14:59:54.699016+0800 XYT[13100:4041493] <Weex>[log]WXJSCoreBridge.m:145, jsLog: store.state.count:  1
2018-12-11 14:59:54.707954+0800 XYT[13100:4041493] <Weex>[error]WXMonitor.m:221, [native-bundle-main.js:1:29760] TypeError: JSON.stringify cannot serialize cyclic structures.
stringify@[native code]

weex -v:

➜ ~ weex -v
   v1.3.11
 - weexpack : v1.2.7
 - weex-builder : v0.4.0
 - weex-previewer : v1.5.1

实现逻辑:
前提:没使用vue-loader
修改webpack.common.conf.js

const relativeStorePath = path.join(relativeVuePath, '../store/index.js')
if (relativeVuePath.indexOf('index') > -1) {
    contents += `import App from '${relativeVuePath}'
import store from '${relativeStorePath}'

new Vue(Vue.util.extend({el: '#root', store}, App));
`;
  }

得到的结果 weex项目下 .temp文件生成如下

import App from '../src/index.vue'
import store from '../src/store/index.js'

new Vue(Vue.util.extend({el: '#root', store}, App));

store文件:

import Vuex from 'vuex'

console.log(111)
// Vuex is auto installed on the web
if (WXEnvironment.platform !== 'Web') {
  Vue.use(Vuex)
}

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  }
})

store.commit('increment')
console.log('store.state.count: ', store.state.count) // -> 1
export default store
阅读 2.6k
1 个回答

这个问题出现的原因是我在 index.vue中的created使用了 console.log(this),去掉就没事了。
引用vuex需要使用import { mapState ,mapMutations,mapGetters,mapActions} from 'vuex' 直接点this在data也会有同样的问题。
所以weex中 .vue文件使用this有风险需要注意

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