3.1 vuex概念
https://vuex.vuejs.org/zh/installation.html
vuex 是 Vue 配套的 公共数据管理工具,它可以把一些共享的数据保存到 vuex 中,方便 整个程序中的任何组件直接获取或修改我们的公共数据;
3.2 vuex的基本使用
#1.拷贝原项目中除了node_modules之外的其他信息,运行npm install安装模块#2.安装vuexnpm i vuex -S#3.main.jsimport Vue from 'vue'import Vuex from 'vuex'// 注册vuex到vue中Vue.use(Vuex)// new Vuex.Store()实例,得到一个数据仓储对象// 可以在组件中通过this.$store.state.xx 来访问store中的数据var store = new Vuex.Store({//state相当于组件中的datastate: {count: 0},//如果要修改store中state的值,需要调用 mutations提供的方法,可以通过this.$store.commit('方法名')来调用mutations: {increment(state) {state.count++},//mutations函数参数列表中最多支持两个参数,其中参数1是state; 参数2是通过commit提交过来的参数;subtract(state, obj) {console.log(obj)state.count -= obj.step;}},getters: {//这里的getters只负责对外提供数据,不负责修改数据,如果想要修改 state 中的数据需要在mutations中修改optCount: function (state) {return '当前最新的count值是:' + state.count}}})// 总结:// 1. state中的数据,不能直接修改,如果想要修改,必须通过 mutations// 2. 如果组件想要直接 从 state 上获取数据: 需要 this.$store.state.***// 3. 如果组件想要修改数据,必须使用 mutations 提供的方法,需要通过 this.$store.commit('方法的名称', 唯一的一个参数)// 4. store中state上的数据在对外提供的时候建议做一层包装,推荐使用 getters。调用的时候则用this.$store.getters.***import App from './App.vue'const vm = new Vue({el: '#app',render: c => c(App),//将vuex创建的store挂载到VM实例上,只要挂载到了 vm 上,任何组件都能使用store来存取数据store})#4.index.html<body><div id="app"></div></body>#5.App.vue<template><div><h1>这是 App 组件</h1><hr><counter></counter><hr><amount></amount></div></template><script>import counter from "./components/counter.vue";import amount from "./components/amount.vue";export default {data() {return {};},components: {counter,amount}};</script>#6.components/amount.vue<template><div><h3>{{ $store.getters.optCount }}</h3></div></template>#7.components/counter.vue<template><div><input type="button" value="绑定事件-减少" @click="sub"><input type="button" value="绑定事件-增加" @click="add"><br><input type="text" v-model="$store.state.count"></div></template><script>export default {data() {return {};},methods: {add() {this.$store.commit("increment");},sub() {this.$store.commit("subtract",{ step:3});}}};</script>
3.3 vuex异步修改状态
mutation的使用局限:mutation必须是同步函数,如果有异步操作,可以在actions完成
#main.js中给store添加actions的方法var store = new Vuex.Store({// state相当于组件中的datastate: {count: 0},// 如果要修改store中state的值,需要调用 mutations提供的方法,可以通过this.$store.commit('方法名')来调用mutations: {increment (state) {state.count++},// mutations函数参数列表中最多支持两个参数,其中参数1是state; 参数2是通过commit提交过来的参数;subtract (state, obj) {console.log(obj)state.count -= obj.step}},getters: {// 这里的getters只负责对外提供数据,不负责修改数据,如果想要修改 state 中的数据需要在mutations中修改optCount: function (state) {return '当前最新的count值是:' + state.count}},actions: {// action中的方法允许异步事件increment ({ commit }) {setTimeout(() => {// 提交mutations中的increment方法commit('increment')}, 1000)},subtract ({ commit }, obj) {// 返回一个Promise,以便做回调return new Promise((resolve, reject) => {setTimeout(() => {commit('subtract', obj)resolve()}, 1000)})}}})#components/counter.vuemethods: {add () {// dispatch 触发指定的actionthis.$store.dispatch("increment")},sub () {this.$store.dispatch("subtract", { step: 3 }).then(() => {console.log("减少操作完成")})}}
3.4 vuex的模块
#1.使用Vuex.Store中的modules声明多个模块const moduleA = {state: {count: 1},mutations: {}}const moduleB = {state: {count: 11},mutations: {}}const store = new Vuex.Store({modules: {a: moduleA,b: moduleB}})#2.使用不同模块中的数据<div><h3>{{ $store.state.a.count }}</h3><h3>{{ $store.state.b.count }}</h3></div>
3.5 刷新网页时vuex缓存
https://blog.csdn.net/weixin_42233917/article/details/82217596
