• 技术文章 >web前端 >Vue.js

    Vuex Module-状态仓库分割的使用介绍

    藏色散人藏色散人2022-08-10 16:01:01转载280

    vuex构成

    vuex主要包含以下五个部分:

    vuex的modules使用

    创建目录

    在这里插入图片描述

    大前端成长进阶课程:进入学习

    在此示例中,我创建了两个store文件,分别是 profile.jscustom.js,一个根文件index.js

    custom.js

    const customs = {
        namespaced: true, // 创建命名空间
        state: { // 存储变量
            showAlert: false
        },
        mutations: { // 定义修改state方法
            CHANGESHOW: (state, params) => {
                state.showAlert = !state.showAlert        }
        },
        actions: { // 异步调用mutations
            setShow: ({ commit }) => {
                commit('CHANGESHOW')
            }
        },
        getters: { // 将数据过滤输出
            bodyShow: state => state.showAlert    }}export default customs

    profile.js

    const profile = {
      namespaced: true,
      state: {
        name: 'common name',
        age: 18,
        bool: false
      },
      mutations: {
        CHANGEMSG: (state, params) => {
          state.name = params    },
        CHANGEAGE: (state, params) => {
          state.name = params    },
        CHANGEBOOL: (state) => {
          state.bool = !state.bool    }
      },
      actions: {
        setName: ({ commit }) => {
          commit('CHANGEMSG', 'Vuex common name')
        },
        setAge: ({ commit }) => {
          commit('CHANGEAGE', 81)
        },
        setBool: ({ commit }) => {
          commit('CHANGEBOOL')
        }
      },
      getters: {
        vuexName: state => state.name,
        vuexAge: state => state.age,
        vuexBool: state => state.bool  }}export default common

    index.js

    import Vue from 'vue'
    import Vuex from 'vuex'
    
    // 引入子store
    import profile from './modules/profile'
    import customs from './modules/customs'
    
    // Vue.use(Vuex)
    const store = new Vuex.Store({
      modules: {
        profile,
        customs
      }
    })
    
    export default store // 导出store,以便于后续使用

    在需要使用的.vue文件里进行使用。方法如下

    index.vue

    <template>
    	<div>
    	  name: <h5>{{vuexName}}</h5> <button @click='setName'>chenge name</button>
          age: <h5>{{vuexAge}}</h5> <button @click='setAge'>chenge age</button>
          bool: <h5>{{vuexBool}}</h5> <button @click='setBool'>chenge bool</button>
          <br/>
          
          <span @click='setShow' style='display:inline-block;width:200px;height:30px;border:1px solid #999;border-radius:5px;text-align:center;line-height:30px;cursor: pointer;'>click me ,change showAlert</span>
          <em>{{bodyShow}}</em>
    	</div>
    </template>
    <script>
    import { mapActions, mapGetters } from 'vuex'
    export default {
    computed: {
        ...mapGetters('profile', ['vuexName', 'vuexAge', 'vuexBool']),
        ...mapGetters('customs', ['bodyShow'])
      },
    methods: {
        ...mapActions('customs', ['setShow']),
        ...mapActions('profile', ['setName', 'setAge', 'setBool']),
    }
    </script>
    <style>
    
    </style>

    app.js

    import Vue from 'vue';
    import VueRouter from 'vue-router';
    // style
    import './../../sass/app.scss';
    
    // Components
    import Main from './Main.vue';
    import routes from './routes';
    // store
    import store from './store';  // 将store挂载到Vue
    
    Vue.use(VueRouter);
    
    const router = new VueRouter({
      routes,
      saveScrollPosition: true,
    });
    
    new Vue({ router, store, ...Main }).$mount('#app');

    初始效果图 ⬇️
    在这里插入图片描述
    点击按钮之后效果图 ⬇️
    在这里插入图片描述
    至此,modules使用流程演示完毕!【相关推荐:vue.js视频教程

    以上就是Vuex Module-状态仓库分割的使用介绍的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:csdn,如有侵犯,请联系admin@php.cn删除

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    快捷开发Web应用及小程序:点击使用

    支持亿级表,高并发,自动生成可视化后台。

    专题推荐:VueX Module
    上一篇:Vue计算属性computed可以做什么?应用场景浅析 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 通俗易懂!详解VUEX状态仓库管理• 一文解析VUEX getters计算属性的基本使用• Vuex状态管理之Mutation的使用详解• Vuex状态管理之Action异步操作详解
    1/1

    PHP中文网