vue.js - vuex 状态预取不起作用
问题描述
我面临一个关于 vuex 的问题,在我的 vue 组件中,我发送了一个存储请求,然后在计算的钩子中我映射了存储状态,并且第一次返回 null 时,我认为它在存储状态之前开始映射。
这是代码index.vue
文件代码,
export default {
name: 'tab_3',
data () {
return {
tableData: []
}
},
beforeCreate () {
this.$store.dispatch('registerWeb3')
this.$store.dispatch('getContractInstance')
},
computed: mapState({
coinbase: state => state.web3.coinbase,
balance: state => state.web3.balance
}),
methods: {
Func: function () {
console.log(this.coinbase)
console.log(this.balance)
}
},
beforeMounte () {
this.Func()
},
updated () {
}
}
这是store.js
文件代码:
Vue.use(Vuex)
export const store = new Vuex.Store({
strict: true,
state,
mutations: {
registerWeb3Instance (state, payload) {
let result = payload
let web3Copy = state.web3
web3Copy.coinbase = result.coinbase
web3Copy.balance = parseInt(result.balance, 10)
},
registerContractInstance (state, payload) {
state.MyContract = () => payload[0]
state.TokenContract = () => payload[1]
}
},
actions: {
registerWeb3 ({commit}) {
getWeb3.then(result => {
commit('registerWeb3Instance', result)
}).catch(e => {
console.log('error in action registerWeb3', e)
})
},
getContractInstance ({commit}) {
getContract.then(result => {
commit('registerContractInstance', result)
}).catch(e => console.log(e))
}
}
})
解决方案
推荐阅读
- c# - 有没有一种更高效的方法来使用反射从循环内的 propertyName 获取类的属性值?
- powerbi - 在 DAX 中计数/计算/求和行
- python - 将字符打印与特定值一样多次
- python - 使用 Python,选择最近 60 天内修改过的目录中的文件
- abap - 如何在双击事件中获取 SALV ALV 字段值?
- python - 在pytorch模型训练中,如何冻结、解冻和再次冻结一些参数?
- javascript - Power BI 自定义主题文件未与 Angular 一起应用
- javascript - 如何在响应对象NodeJs的模板文字中插入html标签
- php - 将选定行的 id 传递给刀片文件中的组件标签
- angular - 在孩子中使用父属性(高度,宽度)