博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vuex
阅读量:7081 次
发布时间:2019-06-28

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

vuex:集中式管理数据http://vuex.vuejs.org/安装项目:cnpm install安装vuex: cnpm install vuex -Dvuex提供个两个方法:    mapActions:管理所有事件    mapGetters:获取数据

main,js

import Vue from 'vue'import App from './App.vue'import store from './store'new Vue({    store,    el: '#app',    render: h => h(App)})

App.vue

store.js

import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex);var state = {    count: 10   //不能有分号};const mutations = {
//处理const actions后的操作 increment(state) { //处理状态(数据)变化 state.count++; }, decrement(state) { state.count--; }};const actions = {
//接受页面的点击事件 increment: ({ //处理你要干什么,异步请求,判断,流程控制 commit }) => { commit('increment') //commit是函数,提交给mutations的increment(state) }, decrement: ({ commit }) => {
//不用箭头函数也可以 commit('decrement'); }, clickOdd: ({ commit, state }) => { if (state.count % 2 == 0) { commit('increment')//提交动作 } }, clickAsync: ({ commit }) => { new Promise((resolve) => {
//Promise是es6的 setTimeout(function() { commit('increment'); resolve();//继续向下走 }, 1000); }); }};const getters = { //页面获取{
{count}},{
{getOdd}} count(state) { return state.count; }, getOdd(state) { return state.count % 2 == 0 ? '偶数' : '奇数'; }};//需要导出Store对象export default new Vuex.Store({ state, mutations, actions, getters});

 

转载地址:http://kslml.baihongyu.com/

你可能感兴趣的文章
几何画板二次函数系的制作
查看>>
[设计模式]PHP设计模式之单例模式
查看>>
树的高度
查看>>
iOS.KVC.setValue:forKey:
查看>>
转:设计模式六大原则(3):依赖倒置原则
查看>>
转:快速排序的一个小问题没想明白,求助各位
查看>>
eclipse rcp 打包出适合不同操作系统和操作位数.
查看>>
简单的个人博客建站
查看>>
使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)...
查看>>
POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用
查看>>
python3中input()方法报错traceback变量未定义的解决方法
查看>>
animation渐进实现点点点等待效果实例页面
查看>>
配置 ssh无密码登陆
查看>>
java读取和写入浏览器Cookies
查看>>
熟悉常用的HDFS操作
查看>>
SCM软件配置管理 (一)SVN 与 CVS
查看>>
js闭包
查看>>
CocoaPods Setting up CocoaPods master repo无反应时的处理
查看>>
linux驱动系列之s3c2440内存布局
查看>>
asp.net上传文件大小限制
查看>>