Vuex

作者: Cherry丶小丸子 | 来源:发表于2023-02-08 11:27 被阅读0次
import { createStore } from 'vuex'

export default createStore({
    // 存储状态(变量)
    state: {
        cacheViews: ['newTemplate', 'editTemplate'],
        todos: [
            { id: 1, text: '你好啊', done: true },
            { id: 2, text: '赛利亚', done: false }
        ],
        count: 1
    },
    // 对数据获取之前的再次编译,可以理解为 state 的计算属性,在组件中通过 this.$store.getters.xxx 使用
    getters: {
        cacheViews: state => state.cacheViews,
        changeTodos: state => state.todos.filter(todo => todo.done),
        getTodoById: state => (id: number) => {
            // 通过方法访问, 给 getter 传参, 在页面中使用 this.$store.getters.getTodoById(2); // 输出结果为: { id: 2, text: '赛利亚', done: false }
            return state.todos.find(todo => todo.id === id)
        }
    },
    // 修改状态,同步操作,在组件中通过 this.$store.commit('xxx', params) 使用
    mutations: {
        increment (state) {
            // 变更状态, this.$store.commit('increment');
            state.count++
        },
        increment2(state, payload){
            // 变更状态, this.$store.commit('increment2', 10);
            state.count += payload;
        },
        increment3(state, payload){
            // 变更状态, this.$store.commit('increment3', { amount: 10 });
            // 对象风格的提交方式 this.$store.commit({ type: 'increment2', amount: 10 });
            state.count += payload.amount;
        }

    },
    // 提交 mutation, 异步操作,在组件中通过 this.$store.dispath('xxx') 使用
    actions: {
        incrementAsync (context) {
            setTimeout(() => {
                context.commit('increment')
            }, 1000)
        },
        incrementAsync2 (context, payload) {
            setTimeout(() => {
                context.commit('increment', payload.amount)
            }, 1000)
        }
    },
    // store 的子模块,为了开发大型项目,方便状态管理而使用的,即将 store 分割为模块,使 store 对象不会太臃肿
    modules: {
        a: moduleA,
        b: moduleB
    }
})

import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'

computed: {
    localComputed () { /* ... */ },
    // 使用对象展开运算符将此对象混入到外部对象中
    ...mapState({
        // ...
    }),
    ...mapGetters({
        // ...
    }),
},
methods: {
    ...mapMutations([
        'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`

        // `mapMutations` 也支持载荷:
        'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
    ]),
    ...mapMutations({
        add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
    }),


    ...mapActions([
        'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`

        // `mapActions` 也支持载荷:
        'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
    ]),
    ...mapActions({
        add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
    })
}

相关文章

  • VUEX基本介绍,包含实战示例及代码(基于Vue2.X)

    VUEX 1 VUEX基本介绍 1.1 官方API 1.2 什么是vuex 1.3 Vuex使用场景 1、Vuex...

  • 【文档笔记】-Vuex

    什么是vuex? vuex文档: https://vuex.vuejs.org/zh/[https://vuex....

  • vuex配置

    vuex配置 目录 vuex的五个核心 配置vuex vuex持久化 一、vuex五个核心概念 State 定义状...

  • Vuex

    安装Vuex cnpm install vuex --save-dev Vuex是什么? 这是[Vuex的官网](...

  • Vuex

    1.Vuex概述 2.Vuex基本使用 3.使用Vuex完成todo案例 1.Vuex概述 Vuex是实现组件全局...

  • vuex

    Vuex介绍: Vuex官网:http://vuex.vuejs.org/ Vuex是实现数据状态管理的技...

  • vuex+axios 的开发流程记录

    相关文档 vuex: https://vuex.vuejs.org/zh/ 是否有必要使用vuex vuex是vu...

  • 2019-06-07

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

  • 配置 vuex 和 vuex 本地持久化

    配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...

  • vuex

    配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...

网友评论

      本文标题:Vuex

      本文链接:https://www.haomeiwen.com/subject/vujtprtx.html