Vuex基础

https://vuex.vuejs.org/zh-cn

state --> view --> action -> state

多组件共享状态, 之前操作方式,由父组件传递到各个子组件。 当路由等加入后,会变得复杂。 引入viewx 解决共享问题。

原vue结构图

vuex结构图

Vuex对象结构 (state,mutations,getters,actions)

state 对象数据

mutations 操作变更state数据

getters 计算state

actions  触发mutations

★学习之后分析数据流向图★

安装

npm install --save vuex

调试

目标

代码1  :原vue实现计数器

app.uve

<template>
<div>
<p>点击次数{{count}}, 奇偶数:{{eventOrOdd}}</p>
<button @click="increment">+</button>
<button @click="decrement">-</button>
<button @click="incrementIfOdd">奇数加</button>
<button @click="incrementAsync">异步加</button>
</div>
</template> <script>
export default {
data () {
return {
count: 0
}
},
computed: {
eventOrOdd () {
return this.count % 2 === 0 ? '偶数' : '奇数'
}
},
methods: {
increment () {
const count = this.count
this.count = count + 1
},
decrement () {
const count = this.count
this.count = count - 1
},
incrementIfOdd () {
const count = this.count
if (count % 2 === 1) {
this.count = count + 1
}
},
incrementAsync () {
setTimeout(() => {
const count = this.count
this.count = count + 1
}, 1000)
}
}
}
</script> <style> </style>

代码2: VUEX实现

store.js

/**
* Created by infaa on 2018/9/22.
*/
import Vue from 'vue'
import Vuex from 'vuex' Vue.use(Vuex) const state = { // 初始化状态
count: 0
}
const mutations = {
INCREMENT (state) {
state.count++
},
DECREMENT (state) {
state.count--
}
} const actions = {
increment ({commit}) {
commit('INCREMENT')
},
decrement ({commit}) {
commit('DECREMENT')
},
incrementIfOdd ({commit, state}) {
if (state.count%2 === 1) {
commit('INCREMENT')
}
},
incrementAsync ({commit}) {
setTimeout( () => {
commit('INCREMENT')
}, 1000)
}
} const getters = {
eventOrOdd (state) {
return state.count % 2 === 0 ? '偶数' : '奇数'
}
} export default new Vuex.Store({
state, // 状态对象
mutations, // 更新state函数的对象
actions,// dispatch 对应actiong
getters // 对应computed 中getters
})

main.js

/**
* Created by infaa on 2018/9/19.
*/
import Vue from 'vue'
import App from './App'
import store from './store' /* eslint-disable no-new */ new Vue({
el: '#app',
components: {App},
template: '<App/>',
store // 所有组件对象多了一个属性$store
})

app.vue

<template>
<div>
<!--<p>点击次数{{count}}, 奇偶数:{{eventOrOdd}}</p>-->
<p>点击次数{{$store.state.count}}, 奇偶数:{{eventOrOdd}}</p>
<button @click="increment">+</button>
<button @click="decrement">-</button>
<button @click="incrementIfOdd">奇数加</button>
<button @click="incrementAsync">异步加</button>
</div>
</template> <script>
export default {
// data () {
// return {
// count: 0
// }
// },
computed: {
eventOrOdd () {
// return this.count % 2 === 0 ? '偶数' : '奇数'
return this.$store.getters.eventOrOdd // js中要写this,模版中不用直接写$store
}
},
methods: {
increment () {
this.$store.dispatch('increment')
},
decrement () {
// const count = this.count
// this.count = count - 1
this.$store.dispatch('decrement')
},
incrementIfOdd () {
this.$store.dispatch('incrementIfOdd')
// const count = this.count
// if (count % 2 === 1) {
// this.count = count + 1
// }
},
incrementAsync () {
this.$store.dispatch('incrementAsync')
// setTimeout(() => {
// const count = this.count
// this.count = count + 1
// }, 1000)
}
}
}
</script> <style> </style>

代码3 优化app.js

app.uve 如果对应名不同,由[ ] 改为{}即可

<template>
<div>
<!-- <h2>点击的个数:{{count}}</h2> -->
<!-- <h3>{{eventOrOdd}}</h3> -->
<!-- Vuex 版本 -->
<h2>点击次数{{$store.state.count}}, 奇偶数:{{eventOrOdd}}</h2>
<button @click="increment">+</button>
<button @click="decrement">-</button>
<button @click="incrementIfOdd">奇数+</button>
<button @click="incrementAsync">异步加</button>
</div>
</template> <script type="text/ecmascript-6">
import {mapState, mapGetters, mapActions} from 'vuex'
// export default{
// data(){
// return{
// count:0
// }
// },
// computed:{
// eventOrOdd(count){
// // return this.count % 2 === 0?'偶数':'奇数'
// return this.$store.getters.eventOrOdd
// } // },
// methods:{
// increment () {
// // const count = this.count;
// // this.count = count+1 // this.$store.dispatch('increment')
// },
// decrement (){
// // const count = this.count;
// // this.count = count -1
// this.$store.dispatch('decrement')
// },
// incrementIfOdd () {
// // const count = this.count;
// // if(count % 2 === 1 ){
// // this.count = count + 1
// // }
// this.$store.dispatch('incrementIfOdd');
// },
// incrementAsync () {
// // setTimeout(() => {
// // const count = this.count
// // this.count = count + 1
// // }, 1000)
// this.$store.dispatch('incrementAsync')
// }
// }
// };
// 优化 App.vue 代码格式
export default{
computed:{
...mapState['count'],
...mapGetters['eventOrOdd']
},
methods:{
...mapActions(['increment','decrement','incrementIfOdd','incrementAsync'])
}
}; </script> <style type="stylus" rel="stylesheet/stylus"> </style>

官方另一个案例购物车, store以目录结构呈现

https://github.com/vuejs/vuex/tree/dev/examples/shopping-cart

 

Vue--- VueX基础 (Vuex结构图数据流向)1.1的更多相关文章

  1. Vuex 基础

    其他章节请看: vue 快速入门 系列 Vuex 基础 Vuex 是 Vue.js 官方的状态管理器 在vue 的基础应用(上)一文中,我们已知道父子之间通信可以使用 props 和 $emit,而非 ...

  2. 解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function

    Vue的项目中,如果项目简单, 父子组件之间的数据传递可以使用  props 或者 $emit 等方式 进行传递 但是如果是大中型项目中,很多时候都需要在不相关的平行组件之间传递数据,并且很多数据需要 ...

  3. 【整理】解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function

    解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function https://www.cnblogs.com/jaso ...

  4. vue进阶:vuex(数据池)

    非父子组件传值 vuex 一.非父子组件传值 基于父子组件通信与传值实现非父子组件传值的示例关键代码: <template> <div> <!-- 学员展示 --> ...

  5. 十、Vue:Vuex实现data(){}内数据多个组件间共享

    一.概述 官方文档:https://vuex.vuejs.org/zh/installation.html 1.1vuex有什么用 Vuex:实现data(){}内数据多个组件间共享一种解决方案(类似 ...

  6. Vue刷新页面VueX中数据清空了,怎么重新获取?

    Vue刷新页面VueX数据清空了,怎么重新获取? 点击打开视频讲解更详细 在vue中刷新页面后,vuex中的数据就没有了,这时我们要想使用就要重新获取数据了, 怎么在刷新后重新获取数据呢??? 这时我 ...

  7. 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十三║Vue实战:Vuex 其实很简单

    前言 哈喽大家周五好,马上又是一个周末了,下周就是中秋了,下下周就是国庆啦,这里先祝福大家一个比一个假日嗨皮啦~~转眼我们的专题已经写了第 23 篇了,好几次都坚持不下去想要中断,不过每当看到群里的交 ...

  8. vuex 基础:教程和说明

    作者注:[2016.11 更新]这篇文章是基于一个非常旧的 vuex api 版本而写的,代码来自于2015年12月.但是,它仍能针对下面几个问题深入探讨: vuex 为什么重要 vuex 如何工作 ...

  9. 25、vuex改变store中数据

    以登录为例: 1.安装vuex:npm install vuex --save 2.在main.js文件中引入: import store from '@/store/index.js'new Vue ...

随机推荐

  1. 关于Activity

    Activity与界面 1.Activity相当于浏览器的标签.相当于空白的网页,界面相当于浏览器内的网页. 2.将Activity与界面绑定就相当于在浏览器内填写了相应的网页. 3.Activity ...

  2. JavaScript简易动画

    <p id="s">fly</p> <script> function move(){ var id=document.getElementBy ...

  3. ASP.NET MVC 音乐商店 - 5 通过支架创建编辑表单 续

    查看 StoreManager 控制器的代码 现在,Store Manager 控制器中已经包含了一定数量的代码,我们从头到尾重新过一下. 首先,在控制器中包含了标准的 MVC 控制器的代码,为了使用 ...

  4. 获取cookie信息

    随着网络安全(例如:登录安全等)要求的不断提升,越来越多的登录应用在登录时添加了验证码登录,而验证码生成算法也在不断的进化,因而对含登录态的自动化测试脚本运行造成了一定程度的困扰,目前解决此种问题的方 ...

  5. 我是一只IT小小鸟读书笔记3

    Part6: 一.    无论在什么时候,师兄师姐都是我们最好的资源,遇到不会的问题一定要及时向他们请教,善于利用身边的人脉关系也是一个基本的技能. 二.    爱好很多,但特长一定要有.仔细思考一下 ...

  6. Mysql 服务无法启动解决办法

    1.我使用的是MySQL-5.7.10-winx64 版本,在安装后启动服务时出现 “服务无法启动”错误 2.解决办法为删除安装目录中的data文件,然后打开cmd调到自己的安装目录下输入mysqld ...

  7. javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/el/ELResolver错误解决办法

    今天不用eclipse.myeclipse等开发工具,纯手写JSP页面(有点作死)时突然出现以前从来没遇到过的问题,报错如下: HTTP Status 500 - java.lang.NoClassD ...

  8. 使用auto_ptr需要注意的事项

    注:C++11 已不推荐使用,应使用scoped_ptr/shared_ptr. 部分原因就是如下的注意事项. 转自:http://patmusing.blog.163.com/blog/static ...

  9. js alert 封装 layui

    方式一: var aaa = function(){ function _alert(aa){ layer.msg(aa, { time: 2000, //2s后自动关闭 alert("最高 ...

  10. idea打jar包经验总结

    关于在idea下打jar问题,在日常工作中经常用到,这里总结下流程. 1.在项目上鼠标右键 --> Open Module Settings 2.如下图,点击 '+' 3. 选择JAR --&g ...