2019-07-03 20:40:49 336浏览
今天千锋扣丁学堂HTML5培训老师给大家分享一篇关于vuex动态注册方法registerModule的实现,文中通过示例代码介绍的非常详细,下面我们一起来看一下吧。
module.exports = { install(_this) { _this.$store.registerModule(['abc'], { namespaced: true, state: { rightTest: 999 }, actions: { setTest: ({commit}, val) => { commit('putTest', val) } }, mutations: { putTest: (state, val) => { state.rightTest = val; } } }) }, uninstall(_this) { _this.$store.unregisterModule(['abc']) } };
import store from '../../store'; export default { install() { store.registerModule(['abc'], { namespaced: true, state: { rightTest: 999 }, actions: { setTest: ({commit}, val) => { commit('putTest', val) } }, mutations: { putTest: (state, val) => { state.rightTest = val; } } }) }, uninstall() { store.unregisterModule(['abc']) } }
import abc from '../../store/test'; ... created() { // 挂载对应的 store abc.install(this); console.log(this.$store, 'install'); }, destroyed() { // 销毁对应的 store abc.uninstall(this); console.info(this.$store, 'uninstall'); }, methods: { test(){ this.$store.dispatch('abc/setTest', Math.random()); }
【关注微信公众号获取更多学习资料】 【扫码进入HTML5前端开发VIP免费公开课】