import Vue from 'vue'

import Vuex from 'vuex'

Vue.use(Vuex)

首字母不能大写

export default new Vuex.Store({

  state: {

            //这里放全局参数

     调用 this.$store.state.online
     模块调用this.$store.state.a.online

test1:‘1111’,

     test2:‘2222’,
     userId: userJs.getUserId() || "", // 用户id
     username: userJs.getUsername() || "", // 登录名
     name: userJs.getName() || "" // 姓名

  },

  getters: {

      //这里是get方法,并且每次打开浏览器优先执行该方法,获取所有的状态

//不能传递参数 ,只能对state数值进行计算不能修改

调用this.$store.getters.getTest

    this.$store.getters.计算属性名           // 不分模块
this.$store.getters['模块名/计算属性名'] // 分模块
import { mapGetters } from 'vuex'
   computed: {
        // 不分模块
        ...mapGetters(['计算属性名'])
         
        // 分模块,不重命名计算属性
        ...mapGetters('模块名', ['计算属性名'])
         
        // 分模块,重命名计算属性
        ...mapGetters('模块名', {'新计算属性名': '旧计算属性名'})
    }
             this。计算属性名 使用
      getTest(state) {
         // return state.test1 + state.test2;
      },
//getTest: state =>state.test1 + state.test2,
          多模块多个state 找全局变量还需要分模块找,也可以通过getters把所有模块的共有变量都集成在一起
/*
const getters = {
userInfo: state => state.user.userInfo,
sidebar: state => state.app.sidebar,
visitedViews: state => state.tagsView.visitedViews,
permission_routes: state => state.permission.routes,
dictionaryList: state => state.base.dictionaryList,
}
export default getters

*/

   },

  mutations: {

//mutations中的方法一般用常量(大写)不是常量大小写都行

     //这里是set方法,mutation是修改state的唯一方法。函数中只能执行 同步函数

    mutation调用方法
    this.$store.commit(“mutation函数名”,发送到mutation中的数据)
模块调用this.$store.commit('modulesB/UPDATE_TO_VIP2')
     m_set_ajax_loading (state, bl) {
       state.ajax_loading = bl
    }

  },

  actions: {

     

      action的功能和mutation是类似的,都是去变更store里的state,不过action和mutation有两点不同:
      1.action主要处理的是异步的操作,mutation必须同步执行,而action就不受这样的限制,也就是说action中我们既可以处理同步,也可以处理异步的操作
       action函数中能执行异步函数, mutation函数中不可以
      2.action改变状态,最后是通过提交mutation,就是action只能通过mutation中修改state的值。

      action调用方法
      this.$store.dispatch(‘action中的函数名’,发送到action中的数据)
模块调用this.$store.dispatch('modulesB/UPDATE_TO_VIP2')

  }

})

store

普通加载模块

const store = new Vuex.Store({
modules: {
user: user,
tagsView: tagsView
},
state,
getters,
mutations,
actions
});

==============================================================================
自动加载模块
import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'

Vue.use(Vuex)

// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context('./modules', true, /\.js$/)

// you do not need `import app from './modules/app'`
// it will auto require all vuex module from modules file
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
// set './app.js' => 'app'
const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
const value = modulesFiles(modulePath)
modules[moduleName] = value.default
return modules
}, {})

const store = new Vuex.Store({
modules,
getters
})

export default store

vue store用法的更多相关文章

  1. vue store存储commit和dispatch

    vue store存储commit和dispatch this.$store.commit('toShowLoginDialog', true);this.$store.dispatch('toSho ...

  2. Vue props用法详解

    Vue props用法详解 组件接受的选项之一 props 是 Vue 中非常重要的一个选项.父子组件的关系可以总结为: props down, events up 父组件通过 props 向下传递数 ...

  3. vue-learning:41 - Vuex - 第二篇:const store = new Vue.Store(option)中option选项、store实例对象的属性和方法

    vuex 第二篇:const store = new Vue.Store(option)中option选项.store实例对象的属性和方法 import Vuex from 'vuex' const ...

  4. vue setTimeout用法 jquery滚动到某一个div的底部

    //vue 中setTimeOut用法 var $this = this; setTimeout(function(){ $this.goEnd() }, 10); goEnd:function(){ ...

  5. vue better-scroll用法

    滚动位置固定:在vue中通过路由切换页面时组件会自动滚动到顶部,需要监听滚动行为才能让滚动位置固定,better-scroll解决了这个问题. 常用效果:移动端很常见的效果,当滑动右边部分的时候,左边 ...

  6. vue之用法

    一.安装 对于新手来说,强烈建议大家使用<script>引入 二. 引入vue.js文件 我们能发现,引入vue.js文件之后,Vue被注册为一个全局的变量,它是一个构造函数. 三.使用V ...

  7. Vue 基本用法

    Vue的基本用法 模板语法{{ }} 关闭掉 django中提供的模板语法{{ }} 指令系统 v-text v-html v-show和v-if v-bind和v-on v-for v-model ...

  8. Vue基本用法

    在学习Vue的基本用法之前,我们先简单的了解一些es6的语法 let: 特点:1.局部作用域 2.不会存在变量提升 3.变量不能重复声明 const: 特点:1.局部作用域 2.不会存在变量提升 3. ...

  9. vue——store全局存储

    业务场景:刷新页面时,首次拉取所有配置,存储到store状态管理用于全局调用: import Vue from 'vue' import Vuex from 'vuex' import userInf ...

  10. vue插槽用法(极客时间Vue视频笔记)

    vue插槽 插槽是用来传递复杂的内容,类似方法 <!DOCTYPE html> <html lang="en"> <head> <meta ...

随机推荐

  1. WPF项目需要不断更新前台图片时,碰到“System.IO.IOException: 文件“xxx”正由另一进程使用“问题的解决

    问题描述 项目中要求能不断拍照并更新显示图片,使用FileStream在本地创建了图片文件: 当下次重新拍照前删除之前拍过的图片时,提示"System.IO.IOException: 文件& ...

  2. aop切面记日志

    package com.netauth.utils.component; import java.lang.annotation.ElementType; import java.lang.annot ...

  3. 启动项目报错org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found character ‘@‘

    报错信息:org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found character ...

  4. iOS中的三种定时器

    iOS中的三种定时器 NSTimer 一.背景 定时器是iOS开发中经常使用的,但是使用不慎会造成内存泄露,因为NSTimer没有释放,控制器析构函数dealloc也没有调用,造成内存泄露. 二.使用 ...

  5. 记一次SpringBoot整合WebSocket 找不到ServerEndpointExporter类的问题

    package com.mengxiangnongfu.cms.framework.configure; import org.springframework.context.annotation.B ...

  6. 使用yum快速安装mysql-5.7(用于测试)

    1)CentOS 7 下安装 MySQL 5.7 下载并安装MySQL官方的 Yum Repository [wget -i -c http://dev.mysql.com/get/mysql57-c ...

  7. 题解 【POJ3728】The merchant(LCA)

    题意:一棵树有N个城市,每个城市商品价格不一样,Q个询问,问从u出发到达v点,每个城市只能经过一次的最大利润 max min数组存u城到u的第2^i个祖先路径上的最值 答案就是u-v路径上的最大值-最 ...

  8. HCIP-进阶实验05-Eth-Trunk配置部署

    HCIP-进阶实验06-Eth-Trunk配置部署 1 实验需求 1.1 实验拓扑 1.2 实验环境说明 无 1.3 实验需求 本实验共采用3台三层交换机.1台路由器.认真分析实验需求,明确每步考查的 ...

  9. win10:你需要来自XXXX的权限才能对此文件夹进行更改

    转载请注明来源:https://www.cnblogs.com/Sherlock-L/p/16769720.html 起因 软件运行失败,看报错信息是删除某个文件夹失败了,行吧,我自己来删.找到目标文 ...

  10. BlendCAC: A Smart Contract Enabled Decentralized Capability-Based Access Control Mechanism for the IoT

    摘要 BlendCAC,它是一种分散的.基于联合能力的 AC 机制,可有效保护大规模物联网系统中的设备.服务和信息.引入了基于联合能力的委托模型 (FCDM) 以支持分层和多跳委托.探索了委托授权和撤 ...