zoukankan      html  css  js  c++  java
  • vuex的模块化使用

    store文件如下

     1.modules下文件是模块化的划分,里面的js有state,action,mutations.然后通过

    export default {
    namespaced: true,
    state,
    mutations,
    actions
    }

    方式导出。

    2.index.js中导出的格式如下

    new Vuex.Store({
      modules:{
          app:{
              namespaced:true,
              state:{},
              mutations:{},
              actions:{}
          },
          ...
      },
      getters:{
        sidebar: state => state.app.sidebar,
        size: state => state.app.size,
        device: state => state.app.device,
        sjhnum: state => state.shangjinhui.num,
        ...
      }
    })

    所以index.js中需要先对modules进行处理,通过require.context获取modules下所有js文件,如下

    const modulesFiles = require.context('./modules', true, /.js$/)
    const modules = modulesFiles.keys().reduce((modules, modulePath) => {
      const moduleName = modulePath.replace(/^./(.*).w+$/, '$1')     
      const value = modulesFiles(modulePath)
      modules[moduleName] = value.default                               
      return modules
    }, {})

    然后对getters的处理可以摘出来放到单独js里。

    因为modules的处理,下面相对于普通的使用方式多了个命名空间

    3.获取store里的数据

    1 this.$store.state.命名空间.

    import { mapState } from 'vuex';

    computed:{

    ...mapGetters({

      'getters里定义的key(其实对应的state值)'

    })

    }

    4修改store值

    this.$store.dispatch('命名空间/actions里的函数名',参数数据),
    this.$store.commit('命名空间/mutations里的函数名',参数数据)

  • 相关阅读:
    python爬虫出现的状态码
    FreeSWITCH部署与功能配置
    爬虫读取内容常见的3种方式
    python:3种爬虫的优缺点
    JSON数据解析
    FreeSWITCH与FreeSWITCH对接
    FreeSWITCH添加中文语音
    异步加载技术与逆向工程概念
    word页眉与页脚详解
    修改MyEclipse内存
  • 原文地址:https://www.cnblogs.com/live-to-talk/p/12786970.html
Copyright © 2011-2022 走看看