vue学习-模块化

vuex细分模块modules

1、新建模块中不使用namespaced:true时

mall.js

export default{
    state:{
        mallname:"tataimall"
    },
    mutations:{
        setMallname(state,payload){
            state.mallname=payload;
        }
    }
}

在store的index.js中需要进行以下操作

import forum from './forum'
//vuex细分模块
  modules: {
    mall
  }

此时调用的组件中:

<h1>{{mall.mallname}}</h1>
     <button @click='setMallname("sweetmall")'>点击换名</button>
 <script>
 methods:{
       ...mapActions(['getSome']),
       ...mapMutations(['addAge','setMallname']),
     /*  addAge:function(){
           this.$store.commit('addAge',5)
       }*/
   },
   computed:{
       ...mapState(['username','age','infolist','mall']),
       ...mapGetters(['newage']),
       
 </script>

结果:
在这里插入图片描述

2、新建模块中使用namespaced:true时

forum.js

export default{
    namespaced:true,
    state:{
        forumname:"sweet forum"
    },
    mutations:{
        setForumname(state,payload){
            state.forumname=payload;
        }
    }
}

index.js中

import forum from './forum'
  //vuex细分模块
  modules: {
    mall,
    forum
  }

组件中

<h1>{{forumname}}</h1>
     <button @click='setForumname("hahaha forum")'>点击换名</button>
 <script>
 methods:{
 methods:{
       ...mapActions(['getSome']),
       ...mapMutations(['addAge','setMallname']),
        ...mapMutations('forum',['setForumname'])
     /*  addAge:function(){
           this.$store.commit('addAge',5)
       }*/
   },
   computed:{
       ...mapState(['username','age','infolist','mall']),
       ...mapGetters(['newage']),
       ...mapState('forum',['forumname'])   //可以通过制定模块名选择模块中的属性
   }
       
 </script>

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值