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. 随机颜色,加载loading效果,节流,应用周期函数,wxs

    随机颜色 data: { colorList:[] }, getColor(){ wx.request({ url: 'https://www.escook.cn/api/color', method ...

  2. C++实现链队列相关操作代码

    #include<iostream>#include<cstdlib>using namespace std;#define MAXSIZE 100#define OK 1#d ...

  3. js 对象命名

    JS 标识符的命名规则,即变量的命名规则: 标识符只能由字母.数字.下划线和'$'组成 数字不可以作为标识符的首字符 对象属性的命名规则 通过[]操作符为对象添加属性时,属性名称可以是任何字符串(包括 ...

  4. JMeter压力测试之环境搭建、脚本调试及报错解决方法(Linux版)

    一.环境部署 后续往服务器上传文件,本文中使用的是xftp,因其不是本文所要讲述的重点,这里不做详解. 第一步:安装所需要版本的JDK,本次使用的是JDK 1.8 下载地址:http://www.or ...

  5. CentOS7下MySQL数据的导入和导出

    一.数据导入 (1)进入mysql [root@localhost mysql]# mysql -u root -p (2)转到对应数据库下 mysql> use zenith_star; (3 ...

  6. kunkun

    <html> <head> <title>cxk</title> </head> <body><h1>给设计师:字体 ...

  7. .NET CORE-IIS发布.netcore项目时报错:HTTP错误500.19-Internal Server Error

    最近IIS发布Core3.1项目的时候遇到下面问题,发现是缺少ASP.NET Core Runtime 解决方法:安装.netcore  host 版本

  8. Linux 升级 gcc g++

    Linux 升级 gcc g++ Centos7 上升级 gcc g++ # yum -y install centos-release-scl # yum -y install devtoolset ...

  9. Java基础——Scanner扫描字符数组出现问题

    问题:今天写的一个简易学生信息类出现了如下问题Exception in thread "main" java.util.InputMismatchException: For in ...

  10. Ansible基础认识及安装使用详解