},
mutations: {
changeModule1(state, newValue) {
state.moudle1 = newValue
},
},
getters: {
getModules1(state) {
return state.moudle1
}
},
actions: {
useActionChangeModule1({
commit
}, newValue) {
commit('changeModule1', newValue)
},
}
}
export default module1
modules2.js
const state = {
moudle2Value: 'module2-value',
}
const mutations = {
changeModule2(state, newValue) {
state.moudle2 = newValue
},
}
const getters = {
getModule2(state) {
return state.moudle2
}
}
const actions = {
useActionChangeModule2({
commit
}, newValue) {
commit('changeModule2', newValue)
},
}
export default { state, mutations, actions, getters }
index.js
import Vue from "vue"
import Vuex from "vuex"
import module1 from "./modules/module1"
import module2 from "./modules/module2"
Vue.use(Vuex)
const store = new Vuex.Store({
modules: {
module1,
module2
},
})
export default store;
使用
获取state
computed: {
...mapState({
module1: state => state.module1.moudle1Value,
module2: state => state.module2.moudle2Value,
}),
},
获取getter
computed: {
getModules1() {
return this.$store.getters.getModules1;
}.
...mapGetters(["getModules1"])
},
commit调用
changeModule1() {
this.$store.commit("changeModule1", "new module 1");
},
...mapMutations(["changeModule1"])
命名空间
为什么要有命名空间
vuex允许不同模块有重名的函数,
比如: this.$store.commit(“changeModule1”, “new module 1”);如果这这里的modules1和modules2模块都
有changeModule1这个函数,那么在commit的时候会导致,两个模块的这个重名函数都会调用。
所以都会命名空间来解决重名函数。
写法如下
const module1 ={
namespaced: true,
......
}
获取getter
...mapGetters("module1", ["getModules1"]) // 映射为 this.$store.getters.common.getToken
// // 方式2
这里分享一份由字节前端面试官整理的「2021大厂前端面试手册」,内容囊括Html、CSS、Javascript、Vue、HTTP、浏览器面试题、数据结构与算法。全部整理在下方文档中,共计111道
HTML
-
HTML5有哪些新特性?
-
Doctype作⽤? 严格模式与混杂模式如何区分?它们有何意义?
-
如何实现浏览器内多个标签页之间的通信?
-
⾏内元素有哪些?块级元素有哪些? 空(void)元素有那些?⾏内元 素和块级元素有什么区别?
-
简述⼀下src与href的区别?
-
cookies,sessionStorage,localStorage 的区别?
-
HTML5 的离线储存的使用和原理?
-
怎样处理 移动端 1px 被 渲染成 2px 问题?
-
iframe 的优缺点?
-
Canvas 和 SVG 图形的区别是什么?
JavaScript
-
问:0.1 + 0.2 === 0.3 嘛?为什么?
-
JS 数据类型
-
写代码:实现函数能够深度克隆基本类型
-
事件流
-
事件是如何实现的?
-
new 一个函数发生了什么
-
什么是作用域?
-
JS 隐式转换,显示转换
-
了解 this 嘛,bind,call,apply 具体指什么
-
手写 bind、apply、call
-
setTimeout(fn, 0)多久才执行,Event Loop
-
手写题:Promise 原理
-
说一下原型链和原型链的继承吧
-
数组能够调用的函数有那些?
-
PWA使用过吗?serviceWorker的使用原理是啥?
-
ES6 之前使用 prototype 实现继承
-
箭头函数和普通函数有啥区别?箭头函数能当构造函数吗?
-
事件循环机制 (Event Loop)