vuex是单页面开发中的vue对应使用的状态管理,他可以集中管理应用的所有组件的状态,下面的这个图是vuex的官方文档的中提供的一张图,要使用vuex,首先要安装vuex,通过npm install vuex --save命令安装vuex
1、在安装好vuex后,我们就可以准备使用了,在src文件目录下简历一个store.js 的文件,具体内容如下:
import Vue from ‘vue‘; import Vuex from ‘vuex‘; Vue.use(Vuex); const state={ //要设置的全局访问的state对象 showFooter: true, changableNum:0 //要设置的初始属性值 }; const getters = { //实时监听state值的变化(最新状态) isShow(state) { //承载变化的showFooter的值 return state.showFooter }, getChangedNum(){ //承载变化的changebleNum的值 return state.changableNum } }; const mutations = { show(state) { //自定义改变state初始值的方法,这里面的参数除了state之外还可以再传额外的参数(变量或对象); state.showFooter = true; }, hide(state) { //同上 state.showFooter = false; }, newNum(state,sum){ //同上,这里面的参数除了state之外还传了需要增加的值sum state.changableNum+=sum; } }; const actions = { hideFooter(context) { //自定义触发mutations里函数的方法,context与store 实例具有相同方法和属性 context.commit(‘hide‘); }, showFooter(context) { //同上注释 context.commit(‘show‘); }, getNewNum(context,num){ //同上注释,num为要变化的形参 context.commit(‘newNum‘,num) } }; const store = new Vuex.Store({ state, getters, mutations, actions }); export default store;
在上面代码中,几个比较关键的点需要解释一下,state我把它理解为vue组件中的data部分,这里既是多个组件抽取的公共数据,我们已经可以用this.$store.state.showFooter或this.$store.state.changebleNum在任何一个组件里面获取showfooter和changebleNum定义的值了,但这不是理想的获取方式;vuex官方API提供了一个getters,和vue计算属性computed一样,来实时监听state值的变化(最新状态)。
如果只是定义state的初始值,不改变它不是我们想要的需求,接下来要说的就是mutations了,mutattions也是一个对象,这个对象里面可以放改变state的初始值的方法,具体的用法就是给里面的方法传入参数state或额外的参数,然后利用vue的双向数据驱动进行值的改变,同样的定义好之后也把这个mutations扔进Vuex.Store里面。
可以用 this.$store.commit(‘show‘) 或 this.$store.commit(‘hide‘) 以及 this.$store.commit(‘newNum‘,6) 在别的组件里面进行改变showfooter和changebleNum的值了,但这不是理想的改变值的方式;因为在 Vuex 中,mutations里面的方法 都是同步事务,意思就是说:比如这里的一个this.$store.commit(‘newNum‘,sum)方法,两个组件里用执行得到的值,每次都是一样的,这样肯定不是理想的需求
好在vuex官方API还提供了一个actions,这个actions也是个对象变量,最大的作用就是里面的Action方法 可以包含任意异步操作,这里面的方法是用来异步触发mutations里面的方法,actions里面自定义的函数接收一个context参数和要变化的形参,context与store实例具有相同的方法和属性,所以它可以执行context.commit(‘ ‘)。当然也可以根据es2015的写法,showFooter这个方法也是可以这样写的
showFooter({commit})=>{ //同上注释 commit(‘show‘); },
外部组件里进行全局执行actions里面方法的时候,你只需要用执行
this.$store.dispatch(‘hideFooter‘)
或this.$store.dispatch(‘showFooter‘)
以及this.$store.dispatch(‘getNewNum‘,6) //6要变化的实参
这样就可以全局改变改变showfooter或changebleNum的值了,如下面的组件中,需求是跳转组件页面后,根据当前所在的路由页面进行隐藏或显示页面底部的tabs选项卡
<template> <div id="app"> <router-view/> <FooterBar v-if="isShow" /> </div> </template> <script> import FooterBar from ‘@/components/common/FooterBar‘ import config from ‘./config/index‘ export default { name: ‘App‘, components:{ FooterBar:FooterBar }, data(){ return { } }, computed:{ isShow(){ return this.$store.getters.isShow; } }, watch:{ $route(to,from){ //跳转组件页面后,监听路由参数中对应的当前页面以及上一个页面 console.log(to) if(to.name==‘book‘||to.name==‘my‘){ // to.name来获取当前所显示的页面,从而控制该显示或隐藏footerBar组件 this.$store.dispatch(‘showFooter‘) // 利用派发全局state.showFooter的值来控制 }else{ this.$store.dispatch(‘hideFooter‘) } } } } </script> }else{ this.$store.dispatch(‘hideFooter‘) } } } } </script>
原文:https://www.cnblogs.com/xinzichenxiang/p/10469591.html