文件目录如下:
定义Vuex实例
- /*
- * @Description:
- * @Version: 2.0
- * @Autor: Cookie
- * @Date: 2022-06-30 14:50:11
- * @LastEditors: Zhang
- * @LastEditTime: 2022-07-01 09:53:13
- */
-
-
- // Vuex数据单向流,数据规范性
- import Vuex from 'vuex'
- import defaultState from './state/state'
- import mutations from './mutations/mutaions'
- import getters from './getters/getters'
- import actions from './actions/actions'
- // import Vue from 'vue'
-
- // Vue.use(Vuex)
-
- // const store = new Vuex.Store({
- // state: {
- // count: 0
- // },
- // mutations: {
- // updateCount(state, num) {
- // state.count = num
- // }
- // }
- // })
- // export default store
- const isDev = process.env.NODE_ENV === 'development'
-
- export default () => {
- return new Vuex.Store({
- strict: isDev, //this.$store.state.count = 99; 开发环境限制这样的写法,规范团队
- state: defaultState, //全局模块
- mutations,
- getters,
- actions,
- })
-
- }
创建Vuex实例
- /*
- * @Description:
- * @Version: 2.0
- * @Autor: Cookie
- * @Date: 2022-04-19 09:36:46
- * @LastEditors: Zhang
- * @LastEditTime: 2022-07-01 09:54:26
- */
- // The Vue build version to load with the `import` command
- // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
- import Vue from 'vue'
- import Vuex from 'vuex'
- import App from './App'
- import router from './router'
- import createStore from '@/store/store'
-
- // import VueRouter from 'vue-router'
- // import createRouter from './router/router'
- // Vue.use(VueRouter)
- // const router = createRouter()
-
- Vue.use(Vuex)
- const store = createStore() //建议这样的方式创建,更容易维护
-
-
- Vue.config.productionTip = false
-
- /* eslint-disable no-new */
- new Vue({
- el: '#app',
- router,
- store,
- components: { App },
- template: '<App/>'
- })
state.js代码如下:
- export default {
- count: 222123,
- firstName: 'Cookie',
- lastName: 'Zhang'
- }
调用state中的数据方式有二种
- <div>{{count}}</div>
- <div>{{counter}}</div>
-
- <script>
- import { mapState, mapGetters, mapActions, mapMutations } from "Vuex";
- computed: {
- count() {
- return this.$store.state.count; // 方式一
- },
- ...mapState({ // 方式二,最好的方式是使用对象形式
- // counter: "count" // 别名
- counter: state => state.count
- }),
- // ...mapState(["count"]),
- }
- </script>
getters代码如下:
//getters类似组件中的coumpted,由于后端的数据获取过来我们需要再次组装,当然也可以再组件中写但是多个页面都需要使用就会重复写多次
- export default {
- fullName(state) {
- return `${state.firstName}---${state.lastName}`
- }
- }
调用getter中的数据方式有二种
- <div>{{ fullName }}</div>
-
- <script>
- import { mapState, mapGetters, mapActions, mapMutations } from "Vuex";
- computed: {
- // fullName() { // 方式一
- // return this.$store.getters.fullName;
- // }
- ...mapGetters({ // 方式二,简写
- fullName: "fullName"
- }),
- </script>
mutation代码如下:
mutation只能接受两个参数,第二个参数需封装成obj
- export default {
- updateCount(state, { num, num2 }) { //只能接受两个参数,第二个参数封装成obj
- console.log(num)
-
- console.log(num2);
- state.count = num
- }
- }
调用mutation中的数据方式有二种 (mutation需要操作数据故需放在methods)
Action.js代码如下:
mutations同步的修改数据,actions异步的修改数据如
- // mutations同步的修改数据,actions异步的修改数据如
- export default {
- updateCountAsync(store, data) {
- setTimeout(() => {
- store.commit('updateCount', {
- num: data.num
- })
- }, data.time)
- }
- }
调用Action中的数据方式有二种 (Action需要操作数据故需放在methods)
- <div>{{ counter }}</div>
-
- <script>
- import { mapState, mapGetters, mapActions, mapMutations } from "Vuex";
- mounted() {
- console.log(this.$store);
- this.$store.dispatch("updateCountAsync", { //方式一
- num: 5,
- time: 2000
- });
- this.updateCountAsync({ //方式二 建议使用
- num: 5,
- time: 2000
- });
-
- },
- methods: {
- ...mapActions(["updateCountAsync"]) // 引入
- },
- </script>
定义模块a和b都添加上命名空间,namespaced: true,
- /*
- * @Description:
- * @Version: 2.0
- * @Autor: Cookie
- * @Date: 2022-06-30 14:50:11
- * @LastEditors: Zhang
- * @LastEditTime: 2022-07-01 09:53:13
- */
- import Vuex from 'vuex'
- import defaultState from './state/state'
- import mutations from './mutations/mutaions'
- import getters from './getters/getters'
- import actions from './actions/actions'
- // import Vue from 'vue'
-
- // Vue.use(Vuex)
-
- // const store = new Vuex.Store({
- // state: {
- // count: 0
- // },
- // mutations: {
- // updateCount(state, num) {
- // state.count = num
- // }
- // }
- // })
- // export default store
- const isDev = process.env.NODE_ENV === 'development'
-
- export default () => {
- const store = new Vuex.Store({ //hot moudle
- // return new Vuex.Store({
- strict: isDev, //this.$store.state.count = 99; 开发环境限制这样的写法,规范团队
- state: defaultState, //全局模块
- mutations,
- getters,
- actions,
- modules: {
- a: {
- namespaced: true, //命名空间,不设置在全局的mutations下,提升规范性
- state: {
- text: 1
- },
- mutations: {
- updateText(state, text) {
- console.log('a.stata', state)
- state.text = text
- }
- },
- getters: {
- // textPlus(state) { //只依赖a模块的值
- // return state.text + 1
- // }
- textPlus(state, getters, rootState) { //依赖全局模块的值 ,getters是获取全局的getters,rootState获取全局的state
- // return state.text + rootState.count // 获取顶层模块的
- return state.text + rootState.b.text //获取b模块的
- }
- },
- actions: {
- add({ state, commit, rootState }) {
- // commit('updateText', rootState.count) // commit默认获取本模块a 的mutations
- commit('updateCount', { num: 567 }, { root: true }) // 调用全局的mutations
- }
- }
- },
- b: {
- state: {
- text: 2
- },
- actions: {
- testAction({ state, commit, rootState }) {
- commit('a/updateText', 'test text', { root: true }) // 调用a的mutations
- }
- }
- }
- }
- })
- <div>{{ texta }}</div>
-
- <script>
- import { mapState, mapGetters, mapActions, mapMutations } from "Vuex";
- computed: {
-
- texta() {
- return this.$store.state.a.text;
- }
- }
- </script>
- <div>{{ textPlus }}</div>
-
- <script>
- import { mapState, mapGetters, mapActions, mapMutations } from "Vuex";
- computed: {
- // ...mapGetters(["fullName", "a/textPlus"]),
- ...mapGetters({
- fullName: "fullName"
- textPlus: "a/textPlus"
- }),
- }
- </script>
- <div>{{ texta }}</div>
-
- <script>
- import { mapState, mapGetters, mapActions, mapMutations } from "Vuex";
- mounted() {
- // 多模块Vuex
- this.updateText(123); // 添加了命名空间
- this["a/updateText"](123);
- this["a/add"]();
-
- },
- methods: {
- ...mapMutations(["a/updateText"])
- ...mapActions(["a/add"])
- },
- </script>
- /*
- * @Description:
- * @Version: 2.0
- * @Autor: Cookie
- * @Date: 2022-06-30 14:50:11
- * @LastEditors: Zhang
- * @LastEditTime: 2022-07-01 10:42:45
- */
- import Vuex from 'vuex'
- import defaultState from './state/state'
- import mutations from './mutations/mutaions'
- import getters from './getters/getters'
- import actions from './actions/actions'
- // import Vue from 'vue'
-
- // Vue.use(Vuex)
-
- // const store = new Vuex.Store({
- // state: {
- // count: 0
- // },
- // mutations: {
- // updateCount(state, num) {
- // state.count = num
- // }
- // }
- // })
- // export default store
- const isDev = process.env.NODE_ENV === 'development'
-
- export default () => {
- const store = new Vuex.Store({ //hot moudle
- // return new Vuex.Store({
- strict: isDev, //this.$store.state.count = 99; 开发环境限制这样的写法,规范团队
- state: defaultState, //全局模块
- mutations,
- getters,
- actions,
- plugins: [
- (store) => {
- console.log('my store plugin')
- store.registerModule('c', { //异步加载的时候才需要用到模块c,此时才需注册模块C
- state: {
- text: 31231
- }
- })
- store.watch((state) => state.count + 999, (newCount) => {
- console.log('new count watched:', newCount) //new count watched: 1000 ,count值发生改变会触发这个watch
- })
- store.subscribe((mutation, state) => { //用于Vuex插件中
- console.log(mutation.type) //updateCount 监听到mutation中被调用的方法
- console.log(mutation.payload) // {num: 1, num2: 2} 被传入的值
- })
- store.subscribeAction((action, state) => {
- console.log(action.type) //updateCountAsync 监听到Action中被调用的方法
- console.log(action.payload) // {num: 5}
- console.log(state)
-
- })
- }
- ]
- })
-
- return store
- }
-
-
-
- // Vuex数据单向流,数据规范性
- /*
- * @Description:
- * @Version: 2.0
- * @Autor: Cookie
- * @Date: 2022-06-30 14:50:11
- * @LastEditors: Zhang
- * @LastEditTime: 2022-07-01 10:42:45
- */
- import Vuex from 'vuex'
- import defaultState from './state/state'
- import mutations from './mutations/mutaions'
- import getters from './getters/getters'
- import actions from './actions/actions'
- // import Vue from 'vue'
-
- // Vue.use(Vuex)
-
- // const store = new Vuex.Store({
- // state: {
- // count: 0
- // },
- // mutations: {
- // updateCount(state, num) {
- // state.count = num
- // }
- // }
- // })
- // export default store
- const isDev = process.env.NODE_ENV === 'development'
-
- export default () => {
- const store = new Vuex.Store({ //hot moudle
- // return new Vuex.Store({
- strict: isDev, //this.$store.state.count = 99; 开发环境限制这样的写法,规范团队
- state: defaultState, //全局模块
- mutations,
- getters,
- actions,
- plugins: [
- (store) => {
- console.log('my store plugin')
- store.subscribeAction((action, state) => {
- console.log(action.type) //updateCountAsync 监听到Action中被调用的方法
- console.log(action.payload) // {num: 5}
- console.log(state)
-
- })
- }
- ],
- })
-
- // Vuex 热更替
- if (module.hot) {
- module.hot.accept([
- './state/state',
- './mutations/mutaions',
- './actions/actions',
- './getters/getters'
- ], () => {
- const newState = require('./state/state').default
- const newMutations = require('./mutations/mutaions').default
- const newActions = require('./actions/actions').default
- const newGetters = require('./getters/getters').default
-
- store.hotUpdate({
- state: newState,
- mutations: newMutations,
- getters: newGetters,
- actions: newActions
- })
- })
- }
-
- return store
- }
-
-
-
- // Vuex数据单向流,数据规范性