vue学习经验总结(vuex状态管理)

1.在vuex里面写入

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

export default new Vuex.Store({

  state: {
    msgStatus:true,     //消息状态
  },
  mutations: {
    changeMessageStatus(state,payload){
      state.msgStatus=payload.status
    }
  }
})

state   针对状态存值(初始值)

mutations     针对页面这个值改变时,调用changeMessageStatus来改变

2.在a页面接口拿到这个消息状态的值,存进vuex中

this.getToken(getinstituteCenterApi,{},(res)=>{
            this.infolist=res.data
            this.newMessage=res.data.newMessage
            this.$store.commit('changeMessageStatus',{'status':this.newMessage==0?false:true})
          })

拿到这个消息的状态,可能为0,可能为1,所用用this.$store.commit来调用vuex中mutations里面的changeMessageStatus方法来改变vuex中的值。

3.在a页面中通过computend方法取到vuex的值,到页面进行状态显示

computed: {
     msgStatus(){
      return this.$store.state.msgStatus;
    }
}

到页面渲染判断:

<span class="mynews"></span><span>我的消息</span><span v-if="msgStatus==1" class="redspan"></span>

4.在b页面,修改vuex中的值,改变消息显示的状态

 this.getToken(getmessagelistApi, this.searchInfo, (res) => {
        this.list=res.data.rows
        if(this.list.filter(item=>item.status==0).length==0){
          this.$store.commit('changeMessageStatus',{'status':false})
        }else{
          this.$store.commit('changeMessageStatus',{'status':true})
        }
        // console.log(this.list)
        this.pagination.total = res.data.count;
      });

当满足条件时,通过mutations里面的changeMessageStatus方法来改变vuex中msgStatus的值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值