vuex 操作姿势
Vuex 应用的核心就是 store,它包含着你的应用中大部分的状态 (state)
你不能直接改变 store 中的状态。改变 store 中的状态的唯一途径就是显式地提交 (commit) mutation。
首先举例一个最近看的使用vuex 存取数据状态的 套路写法
建立一个stroe 文件夹中建立一个index.js 初始化vuex 。引入state.js 定义全局定义的数据。mutations.js 是定义改变state值的操作函数,引入getters.js 是获取值前可对值进行操作,类似计算属性。actions.js 一些多个改变state 值的提交函数,或者函数里有异操作createLogger 是vuex 提供的调试工具,在调试的时候可以方便的从控制台看到当前state的改变状态,另建立一个mutation-type.js 来定义一些常亮,来专门定义mutations 的函数名称,方便整理

index.js
import Vue from "vue"
import Vuex from "vuex"
import * as actions from './actions.js'
import * as getters from './getters.js'
import state from './state.js'
import mutations from './mutations.js'
import createLogger from 'vuex/dist/logger' Vue.use(Vuex);
const debug = process.env.NODE_ENV !== 'production' export default new Vuex.Store({
actions,
getters,
state,
mutations,
strict:debug,
plugins:debug ? [createLogger()]:[]
})
state.js 里面设置初始值,就是想全局设置的数据
const state = {
singer:{},
playing:false,
fullScreen:false,
playList:[],
sequenceList:[],
mode:1,
currentIndex:-1,
disc:{},
topList:{},
searchHistory:loadSearch(), //从缓存中取初始值
skinColor:localStorage.skinColor || '#B72712',
}
export default state
getters.js
export const singer = state => state.singer export const playing = state => state.playing;
export const fullScreen = state => state.fullScreen;
export const playList = state => state.playList;
export const sequenceList = state => state.sequenceList;
export const mode = state => state.mode;
export const currentIndex = state => state.currentIndex;
export const currentSong = (state) => {
return state.playList[state.currentIndex] || {}
}
mutation-type.js
export const SET_SINGER = 'SET_SINGER' export const SET_PLAYING_STATE = 'SET_PLAYING_STATE' ...
mutations.js
import * as types from './mutation-types.js'
const mutations = {
[types.SET_SINGER](state, singer) {
state.singer = singer
},
[types.SET_PLAYING_STATE](state, flag) {
state.playing = flag
},
[types.SET_FULL_SCREEN](state, flag) {
state.fullScreen = flag
},
[types.SET_PLAYLIST](state, list) {
state.playList = list
}
}
export default mutations
actions.js
import * as types from './mutation-types.js'
export const selectPlay = function({commit,state},{list,index}){
commit(types.SET_SEQUENCE_LIST, list)
commit(types.SET_CURRENT_INDEX, index)
commit(types.SET_FULL_SCREEN, true)
commit(types.SET_PLAYING_STATE, true)
}
基本套路每增加一个数据状态 就相对应的修改这四个文件
在组件中存储获取vuex 中的数据,用vuex 提供的语法糖语法,获取用mapGetters,提交修改数据用 mapMutations 或者 mapActions
mapGetters 里参数是数组,值对应getters.js 定义的函数名,mapMutations 里参数是对象,设置一个代理方法名方便在组件中使用,mapActions 参数是actions.js 中定义的函数名
import {mapGetters,mapMutations,mapActions} from 'vuex';
computed:{
...mapGetters([
'fullScreen',
'playList',
'currentSong',
])
},
methods:{
...mapMutations({
setFullScreen:"SET_FULL_SCREEN",
setPlayingState:'SET_PLAYING_STATE',
}),
...mapActions([
'saveFavoriteList',
'deleteFavoriteList'
])
},
非语法糖写法
this.$store.state.musicData; //获取state 值
this.$store.commit('changeLinkBorderIndex', 2); //修改mutation
this.$store.dispatch('getData'); // 派发修改actions 方法
* 安装 vue-devtools 可以方便看到vuex 状态

以上均是个人理解,务必请自行看官方文档理解
vuex 操作姿势的更多相关文章
- vuex操作
import Vuex from 'vuex' //引入Vue.use(Vuex) //加载到Vue中//创建一个数据存储对象var store=new Vuex.Store({ //state可以当 ...
- 还看不懂同事的代码?超强的 Stream 流操作姿势还不学习一下
Java 8 新特性系列文章索引. Jdk14都要出了,还不能使用 Optional优雅的处理空指针? Jdk14 都要出了,Jdk8 的时间处理姿势还不了解一下? 还看不懂同事的代码?Lambda ...
- Vuex操作步骤
概念流程图: 案例: (1)src/store/index.js导出仓库 (2)在入口文件引入仓库并派发到每个组件,在入口文件main.js引入,挂载到根组件上,方便以后使用this.$store调用 ...
- 萌新 学习 vuex
vuex官网文档 https://vuex.vuejs.org/zh-cn/ 注: Mutation事件使用commit触发, Actions事件使用dispatch触发 安装 npm install ...
- vuex数据管理-数据模块化
对于vue这类mvvm框架来说,其核心就是组件与数据,因此做好相应的数据管理极为重要.这里分享下vuex数据模块化管理的方法,有利于搭建便于维护.协作的vue项目. vuex管理基本方法和使用 模块化 ...
- Golang的文件处理方式-常见的读写姿势
Golang的文件处理方式-常见的读写姿势 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在 Golang 语言中,文件使用指向 os.File 类型的指针来表示的,也叫做文件句柄 ...
- SpringBoot系列教程web篇Listener四种注册姿势
java web三要素Filter, Servlet前面分别进行了介绍,接下来我们看一下Listener的相关知识点,本篇博文主要内容为SpringBoot环境下,如何自定义Listener并注册到s ...
- SpringBoot系列教程web篇Servlet 注册的四种姿势
原文: 191122-SpringBoot系列教程web篇Servlet 注册的四种姿势 前面介绍了 java web 三要素中 filter 的使用指南与常见的易错事项,接下来我们来看一下 Serv ...
- SpringBoot系列教程JPA之query使用姿势详解之基础篇
前面的几篇文章分别介绍了CURD中的增删改,接下来进入最最常见的查询篇,看一下使用jpa进行db的记录查询时,可以怎么玩 本篇将介绍一些基础的查询使用姿势,主要包括根据字段查询,and/or/in/l ...
随机推荐
- 「Django」rest_framework学习系列-分页
分页a.分页,看第N页,每页显示N条数据方式一:使用PageNumberPagination创建分页对象,配合settings全局配置 views设置 from rest_framework.pagi ...
- poj 3164 Command Network(最小树形图模板)
Command Network http://poj.org/problem?id=3164 Time Limit: 1000MS Memory Limit: 131072K Total Subm ...
- 倍增求lca模板
倍增求lca模板 https://www.luogu.org/problem/show?pid=3379 #include<cstdio> #include<iostream> ...
- 怎样用javascript关闭本窗口
大家都知道window.close()是用来关闭窗口的,而且ie和firefox都是支持的. 为了实现用户对浏览器的绝对控制,ie中用close关闭非open打开的窗口时回弹出一个对话框询问用户,怎么 ...
- [php]apache虚拟主机配置
1.所谓虚拟主机的配置,即url与磁盘目录的绑定 2.在httpd.conf中查询Virtual host,发现有注释说明需要在conf/extra/httpd-vhosts.conf中进行配置. 3 ...
- vue-cli使用说明
一.安装npm install -g vue-cli 推荐使用国内镜像 先设置cnpm npm install -g cnpm --registry=https://registry.npm.taob ...
- c语言学习笔记.预处理.#ifndef
#ifndef -> if not define 配合 #endif使用 在h头文件中使用,防止重复包含和编译. 也可以用条件编译来实现. 例如: 编写头文件 test.h 在头文件开头写上两行 ...
- 仿阿里云后台管理界面模板html源码——后台
链接:http://pan.baidu.com/s/1nuH2SPj 密码:ar8o
- 【IDEA】IDEA设置新建文件的模板
今天在IDEA中新建JS文件的时候想着也像WebStorm一样可以显示作者和时间,所以就研究了下在IDEA中修改文件创建时的模板. 点击settings找到File and Code Template ...
- hash算法搜索获得api函数地址的实现,"kernel32.dll", "CreateThread"
我们一般要获得一个函数的地址,通常采用的是明文,例如定义一个api函数字符串"MessageBoxA",然后在GetProcAddress函数中一个字节一个字节进行比较.这样弊端很 ...