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. for循环当中的 var let区别

    首先要了解这里代码执行顺序: for循环同步:setTimeout异步: js在执行代码的过程中,碰到同步代码会依次执行,碰到异步代码就会将其放入任务队列中进行等待,当同步代码执行完毕后再开始执行异步 ...

  2. HC-SR501人体红外传感器使用说明

    1. 模块为全自动感应,当人进入其感应范围则输出高电平,人离开感应范围则自动延时关闭高电平,输出低电平. 2. 传感器有两种触发方式(可通过跳线进行选择):第一种不可重复触发方式,即感应输出高电平后, ...

  3. 隐藏来源 禁用Referrer 的方法

    原文链接: https://www.cnblogs.com/duanweishi/p/16490197.html https://blog.csdn.net/qq996150938/article/d ...

  4. PHP中的超级变量

    超级变量,又名超级全局变量,是PHP内置的变量,这些变量在代码的任意位置都能正常使用 PHP中的超级变量 9种超级变量 $GLOBALS $_SERVER 9种超级变量 目前,PHP提供了9种超级变量 ...

  5. Oracle 详细-创建用户并导入sql文件

       0.基本信息查询SQL   select * from dba_users; 查看数据库里面所有用户,前提是你是有dba权限的帐号,如sys,system select * from all_u ...

  6. 深入理解webpack的chunkId对线上缓存的思考(转载)

    转载自https://juejin.cn/post/6844903924818771981#heading-6  作者:Kimm 想必经常使用基于webpack打包工具的框架的同学们,无论是使用Rea ...

  7. 090_Java

    在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著地提升程序性能. ● 1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载 ...

  8. python运行脚本报错Non-UTF-8

    写完脚本运行报:SyntaxError: Non-UTF-8 code starting with '\xa1' in file/createuser/test.py on line 1, but n ...

  9. Esp8266 Arduino PubSubClient连接阿里云老是返回rc=2错误的解决方案

    最近在用Esp8266 Arduino连接阿里云IOT的时候,服务器端老是返回rc=2 的错误! 用了MQTTfx模拟连接阿里云IOT没有问题,但是把同样的ClientID,UserName和Pass ...

  10. linux添加分辨率

    由于屏幕分辨率是1920X1080,但是虚拟机中的centos的分辨率设置中没有这个值,因此需要添加一个.在终端中输入如下命令:1.cvt 1920 1080得到: # 1920x1080 59.96 ...