npm i vuex --save
图示:

//引入vuex import Vuex from 'vuex' Vue.use(Vuex); //初始化仓库 const store = new Vuex.Store({ state: { msg: '纸上得来终觉浅', } }) new Vue({ el: '#app', router, // store:store, store, components: { App }, template: '<App/>' })
<p>仓库中的msg为:{{ $store.state.msg }}</p>
<script> import { mapState } from 'vuex' export default { computed:{ // ...mapState(['msg','num']), ...mapState({ str:state=>state.msg, num:state=>state.num }), } } </script> <template> <div> <p>{{ str }}</p> </div> </template>
//初始化仓库 const store = new Vuex.Store({ // 定义初始数据 state: { msg: '纸上得来终觉浅', num:100 }, // 定义计算属性 getters:{ newnum(state){ return state.num + 10; } } })
②在页面组件中使用计算属性的结果
<p>仓库中的newnum为:{{ $store.getters.newnum }}</p>
import { mapGetters } from 'vuex' export default { computed:{ ...mapGetters(['newnum']) } } <template> <div> <p>{{ newnum }}</p> </div> </template>
const store = new Vuex.Store({ ... mutations:{ changeNum(state){ state.num += 10; } } })
接收额外的参数
mutations:{ changeNum(state,n=10){ state.num += n; } }
!-- 不传递参数 --> <button @click="$store.commit('changeNum')">改变仓库中的num-mutations</button> <!-- 传递额外的参数 --> <button @click="$store.commit('changeNum',5)">改变仓库中的num-mutations</button>
<script> import { mapMutations } from 'vuex' export default { methods:{ // ...mapMutations(['changeNum']) //改变仓库的mutations方法默认的名称 ...mapMutations({ setnum:'changeNum' }) } } </script> <template> <div> <!--<button @click="changeNum()">改变仓库中的num-mutation</button>--> <button @click="setnum()">改变仓库中的num-mutation</button> </div> </template>
const store = new Vuex.Store({ ... mutations:{ changeMsg(state){ state.msg = '绝知此事要躬行' } }, // 定义可以执行异步操作的方法 actions:{ //context 上下文,是当前仓库本身 changeMsgSync(context){ //setTimeout(function(){ //提交mutation来实现数据的修改 context.commit('changeMsg') //},3000) } } })
②在页面组件中通过dispatch触发actions中的方法
<button @click="$store.dispatch('changeMsgSync')">改变仓库中的msg-action</button>
<script> import { mapState,mapGetters,mapMutations,mapActions } from 'vuex' export default { methods:{ // ...mapMutations(['changeNum']) ...mapMutations({ setnum:'changeNum' }), ...mapActions(['changeMsgSync']) } } </script> <template> <div> <button @click="changeMsgSync()">改变仓库中的num-actions</button> </div> </template>
modules:{ //模块名称:{} back:{ namespaced:true,//启用命名空间 state:{ num:200 }, mutations:{ changeNum(state){ state.num+=10; } } } }
②在页面组件中使用back模块中的数据
<p>back模块中的num为:{{ $store.state.back.num }}</p>
③在页面组件中调用back模块中改变数据的方法
<button @click="$store.commit('back/changeNum')">改变back模块中的num</button>
npm i vuex-persistedstate
- (2)使用
import createPersistedState from "vuex-persistedstate"; export default new Vuex.Store({ state:{ userinfo:null }, mutations:{ setUserInfo(state,data){ state.userinfo = data; } }, plugins:[createPersistedState()] })
- (3)sessionStorage保存数据
plugins:[createPersistedState({
storage:window.sessionStoreage
})]
npm i element-ui
/src/main.js
import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css';//一定要引入样式文件 Vue.use(ElementUI)
posted on
浙公网安备 33010602011771号