vue store用法
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('模块名', {'新计算属性名': '旧计算属性名'}) } 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用法的更多相关文章
- vue store存储commit和dispatch
vue store存储commit和dispatch this.$store.commit('toShowLoginDialog', true);this.$store.dispatch('toSho ...
- Vue props用法详解
Vue props用法详解 组件接受的选项之一 props 是 Vue 中非常重要的一个选项.父子组件的关系可以总结为: props down, events up 父组件通过 props 向下传递数 ...
- 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 ...
- vue setTimeout用法 jquery滚动到某一个div的底部
//vue 中setTimeOut用法 var $this = this; setTimeout(function(){ $this.goEnd() }, 10); goEnd:function(){ ...
- vue better-scroll用法
滚动位置固定:在vue中通过路由切换页面时组件会自动滚动到顶部,需要监听滚动行为才能让滚动位置固定,better-scroll解决了这个问题. 常用效果:移动端很常见的效果,当滑动右边部分的时候,左边 ...
- vue之用法
一.安装 对于新手来说,强烈建议大家使用<script>引入 二. 引入vue.js文件 我们能发现,引入vue.js文件之后,Vue被注册为一个全局的变量,它是一个构造函数. 三.使用V ...
- Vue 基本用法
Vue的基本用法 模板语法{{ }} 关闭掉 django中提供的模板语法{{ }} 指令系统 v-text v-html v-show和v-if v-bind和v-on v-for v-model ...
- Vue基本用法
在学习Vue的基本用法之前,我们先简单的了解一些es6的语法 let: 特点:1.局部作用域 2.不会存在变量提升 3.变量不能重复声明 const: 特点:1.局部作用域 2.不会存在变量提升 3. ...
- vue——store全局存储
业务场景:刷新页面时,首次拉取所有配置,存储到store状态管理用于全局调用: import Vue from 'vue' import Vuex from 'vuex' import userInf ...
- vue插槽用法(极客时间Vue视频笔记)
vue插槽 插槽是用来传递复杂的内容,类似方法 <!DOCTYPE html> <html lang="en"> <head> <meta ...
随机推荐
- 正点原子sys.h文档详解
文档主体内容为位带的映射,目的是实现位带操作. 第一部分:位带映射的宏函数 1 #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x200 ...
- 0627.selenium请求库*2
昨天已经介绍了关于selenium的简单基本基本使用--通过两种方式自动打开和关闭谷歌浏览器今天,我们将介绍的模块将是在昨天打开谷歌浏览器的基础上,打开某一个电商平台,并搜索某一个产品,利用前面学的知 ...
- vue+element form 动态改变rules校验数据
优化:确定secondRules的数据在secondFlag改变之前进行赋值 可以用$nextTick来执行,不用setTimeOut ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...
- Oracle 详细-创建用户并导入sql文件
0.基本信息查询SQL select * from dba_users; 查看数据库里面所有用户,前提是你是有dba权限的帐号,如sys,system select * from all_u ...
- 应用kafka的经验
Kafka 部署注意事项? 启动用户,非root 安装目录权限:除了数据目录和日志目录是读写外,bin目录是可执行,其他目录应该只读 默认端口修改 只容许内网访问 集成监控和管理软件 开启认证 Kaf ...
- Appium获取元素坐标
文章转自:https://www.cnblogs.com/lfr0123/p/13686769.html appium做app自动化测试过程中,有时需要获取控件元素的坐标进行滑动操作.appium中提 ...
- allure+testng遇到的一些问题
java+testng+allure 听说allure报告,"很好看",决定引入. 首先看allure官网,需要在pom.xml中引入包 文档:https://docs.qamet ...
- USB从机主动断开连接
目录 在一些情况下,需要USB口的引脚复用成其他功能,比如说PB10.PB11要复用成定时器输出引脚:或者需要USB从机主动断开,类似某些杀毒软件带的"安全弹出",不过杀毒软件操作 ...
- C#中实现类型对foreach的支持
代码实现: 首先创建用来遍历的类 class Car { public string name; public int age; } public class Cars: IEnumerable { ...
- page-break-after:always 打印分页
在实现打印功能时候,可以完整分页,需要配合二维数组来使用,避免下一页数据展示到上一页