vuex辅助函数 【vuex进阶使用】

Vuex的介绍

一:基本使用
  1. 新建实例化对象
    const store = new Store.Vuex({})

  2. state => 相当于data
    state:{
    name:‘xxx’
    }
    调用形式:this.$store.state.xxx

  3. getters => 相当于computed
    getters:{
    contactName(state){
    xxx
    }
    }
    调用形式: this.$store.getters.xxx 默认传入state

  4. mutations => 相当于methods
    mutations:{
    changeName(state,[params]){
    xxx
    }
    }
    调用形式 :this.$store.commit(‘functionName’,params)
    state为默认传入的第一个参数,之后才是自己需要传递的数据

  5. actions => 相当于 methods
    action:{
    changeNameAsync(context,[params]){
    xxx
    }
    }
    调用形式: this.$store.dispatch(functionName,params)
    context默认传入 拥有和new Store.Vuex()实例化对象一样的数据和方法

  6. mutations和actions的区别
    mutations和actions都可以对state中的数据进行操作,唯一不同的地方在于mutations中不能进行异步操作
    setTimeOut,setInterval这些异步操作都只能在actions中进行。

  7. modules模块化

const ModuleA = {
    namespaced: true,
    state:{},
    getters:{},
    mutations:{},
    actions:{},
}
const ModuleB = {
    namespaced: true,
    state:{},
    getters:{},
    mutations:{},
    actions:{},
}
const store = nre Store.Vuex({
    modules:{
        //形式一 调用store.state.a/b
        a:moduleA,
        b:moduleB,
        //形式二 调用store.state.moduleA/moduleB
        mudlueA,moduleB
    }
})

二:辅助函数的使用
  1. 引入
    import {mapState,mapGetters,mapMutations,mapActions} from ‘Vuex’
    此处需要注意 Vuex的大小写要与main.js引入时一致 否则会出现警告

  2. 使用
    辅助函数是官方提供的state的语法糖,在引入较多的state值的时候,可以利用此函数简化声明
    state和getters归入到computed中去映射 mutations和actions归入到methods中去映射

//默认形式的Vuex维护
data(){
    return{
        // 如果需要引入的state太多的话 就需要写很多重复的代码
        name:this.$store.state.name,
        age:this.$store.state.age,
        sex:this.$stroe.ModuleA.sex,
        height:this.$store.ModuleB.height,
        mergeName:this.$store.getters.mergeName,
    }
}

辅助函数使用的四种形式(/以mapState为例 其他的辅助函数用法基本类似)

// 形式一 mapState传入的参数为对象
computed:mapState({
    // 原来的计算属性保留
    fn1(){return xxx}
    fn2(){return xxx}
    fn3(){return xxx}

    // 维护Vuex的内容
    // 箭头函数的形式 this指向new Store.Vuex()的实例化对象 store
    name:state => state.name,
    // 键名和state的值名称一致
    age:'age',
    // 正常函数定义的形式 需要使用Vue实例化对象中data的数据时必须使用该形式
    sex(state){
        return this.tips + state.sex
    }
})

// 形式二 mapState传入的参数是数组
computed:mapState([
    'name', //映射 this.name = this.$store.state.name
    'age', 
    'sex', 
    // 此形式要求映射的computed的名称 和 state中属性的名称一致
])

// 形式三 使用...拓展运算符
computed:{
    //原本的计算属性保留
    mergeStr(){xxx} 

    //维护Vuex 此形式将mapState函数返回的对象和原本的内容合并在一起
    ...mapState({xxx}) 
    // 也可以使用...mapState([xxx])的形式 但是在之前的基础上多了一个条件 state的值不能存放在module中 
}

// 形式四 ...配合module
computed:{
    // mapState等辅助函数 第一个参数可以指定module中的模块名称
    // 第二个参数 在进行对应的映射操作
    ...mapGetters('footerStatus', {
        isShow: 'isShow',
    })
    ...mapGetters('collection', {
      allList: 'renderCollects'
    })
}

原文链接

在Vue 3中,你可以使用Vuex辅助函数来简化在组件中使用Vuex的操作。下面是使用Vuex辅助函数的步骤: 首先,确保已经安装了Vuex和Vue 3。 在你的Vue 3项目中,创建一个store文件夹,并在其中创建一个store.js文件。在store.js文件中,你可以定义你的Vuex store,包括状态(state)、mutations、actions等。 ```javascript // store.js import { createStore } from 'vuex'; const store = createStore({ state: { // 状态 }, mutations: { // 修改状态的方法 }, actions: { // 异步操作的方法 }, }); export default store; ``` 在你的根组件(App.vue)中,使用`createApp`函数创建Vue实例时,将store传递给`use`方法,这样整个应用程序都可以访问到store。 ```javascript // App.vue import { createApp } from 'vue'; import store from './store'; createApp(App).use(store).mount('#app'); ``` 现在,你可以在任何组件中使用Vuex辅助函数来访问store中的状态和触发mutations或actions。 在组件中,你可以使用`mapState`辅助函数来将store中的状态映射到组件的计算属性中。 ```javascript // MyComponent.vue import { mapState } from 'vuex'; export default { computed: { ...mapState(['stateProperty']), }, }; ``` 你也可以使用`mapMutations`辅助函数来将mutations映射到组件的methods中,以便在组件中触发mutations。 ```javascript // MyComponent.vue import { mapMutations } from 'vuex'; export default { methods: { ...mapMutations(['mutationName']), }, }; ``` 最后,你可以使用`mapActions`辅助函数将actions映射到组件的methods中,以便在组件中触发actions。 ```javascript // MyComponent.vue import { mapActions } from 'vuex'; export default { methods: { ...mapActions(['actionName']), }, }; ``` 这样,你就可以在Vue 3中使用Vuex辅助函数来简化在组件中使用Vuex了。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值