博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Vue状态管理之Vuex
阅读量:4343 次
发布时间:2019-06-07

本文共 1901 字,大约阅读时间需要 6 分钟。

npm install vuex --save
  • state,驱动应用的数据源;
  • view,以声明方式将state映射到视图;
  • actions,响应在view上的用户输入导致的状态变化。

 

store/index.js

import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)const state = {    userList: [1, 2, 3, 4],    count: 0}const getters = {    getUrlParams: () => () => {        let url = location.search;        let theRequest = {};        if (url.indexOf("?") != -1) {            var str = url.substr(1), strs;            strs = str.split("&");            for (var i = 0; i < strs.length; i++) {                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);            }        }        return theRequest;    },    getUserList: (state) => {        //注:这里会缓存,只有执行了 actions,这里的缓存才会更新        return state.userList;    }}const mutations = {       setUserList(state, data){        // 如果这里的 userList 接口返回,可以用axios请求        state.userList=data;    },    mutationsAddCount(state, n = 0) {        return (state.count += n)    },    mutationsReduceCount(state, n = 0) {           console.log(n)        return (state.count -= n)    }}const actions={    actionsAddCount(context, n = 0) {        console.log(context)        return context.commit('mutationsAddCount', n)    },    actionsReduceCount({ commit }, n = 0) {        return commit('mutationsReduceCount', n)    },    commitUserList:({commit}, userList) => commit('setUserList',userList)}// const actions = {//     commitUserList: ({ commit }, userList) => commit('setUserList', userList)// }const store = new Vuex.Store({    state: state,    getters: getters,    mutations: mutations,    actions: actions})export default store;

demo:

,

demo2:

 

https://www.jianshu.com/p/f393bdd3b03d

转载于:https://www.cnblogs.com/shy1766IT/p/11070646.html

你可能感兴趣的文章
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------&gt;Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>
原生和jQuery的ajax用法
查看>>
iOS开发播放文本
查看>>
20145202马超《java》实验5
查看>>
JQuery 事件
查看>>
main(argc,argv[])
查看>>