vue_vuex
- vue 插件 npm install vuex --save
- 将多个组件的共享状态进行 集中式管理 - 极易破坏单向数据流
- 多个视图依赖于同一状态 ----- 就 props 而言:嵌套组件传参繁琐,兄弟组件传参无能为力
- 来自不同视图的行为需要变更同一状态
将组件的共享状态抽取出来: 组件共享的数据、后台请求数据
- 核心对象 store
向外暴露 store 对象
状态自管理应用 分为
state -------- 驱动应用的 数据源
view -------- 以声明方式将 state 映射到视图 - template
actions -------- 响应有用户在 view 上的用户输入导致的状态变化 - methods: {}
action 中的 commit() 来触发 mutation 的调用,间接更新 state
一个 action 中可能有多个 commit("mutation的名字", 传参给mutation)
开发工具 会监视 mutations
---------------------------------------------------------------------------------------
src/store.js -------- vuex 最核心的管理模块
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
const state = { // 包含所有状态数据的对象,相当于 data
count: 1 // 指定初始状态数据
};
const actions = { // 包含 n 个用于间接更改共享状态数据方法的对象 - 分析对状态数据的几个间接操作
increment ({commit}) {commit("INCREMENT")}
decrement ({commit}) {commit("DECREMENT")}
incrementIfOdd ({commit, state}) {if(state.count%2===1)commit("INCREMENT")}
// 不像 react-thunk 才能实现 redux 的异步,vuex 本身就可以异步
incrementAsync ({commit}) {setTimeout(()=>commit("INCREMENT"), 1000)}
};
const mutations = { // 包含 n 个用于直接更改共享状态数据方法的对象 - 分析对状态数据的几个直接操作
INCREMENT (state) {state.count++}
DECREMENT (state) {state.count--}
};
const getters = { // 包含了 计算属性 的定义
evenOrOdd(state){return state.count % 2 === 0 ? '偶数' : '奇数'}
};
export default new Vuex.Store({
state,
actions,
mutations,
getters
});
---------------------------------------------------------------------------------------
src/main.js
import Vue from 'vue'
import App from './App.vue'
import store from './store/index' /* eslint-disable no-new */
new Vue({
el: '#app',
components: {
App
},
template: '<App/>',
store
})
src/App.vue -------- 组件中使用 vuex 管理的状态数据 - ViexModel 即 组件对象的 this 会多一个 $store 对象
...
methods: {
increment () {this.$store.dispatch("increment")}
decrement () {this.$store.dispatch("decrement")}
incrementIfOdd () {this.$store.dispatch("incrementIfOdd")}
incrementAsync () {this.$store.dispatch("incrementAsync")}
}
...
------------------------------------ 简化写法 ------------------------------------
import {mapState, mapGetters, mapActions} from "vuex"
...
computed: {
...mapState(["count"]), // 函数返回有一个对象,拆包解构以后再放入另一个对象
....mapGetters(["evenOrOdd"])
} // 相当于:
methods: {...mapActions(["increment", "decrement", "incrementIfOdd", "incrementAsync"])} // 用的少?因为只适合只有 this.$store 的函数
...
Vuex.Store({ modules: {...} }) --------- 大型项目,按 功能模块 合理拆分 store
Vuex 允许我们将 store 分割成模块(module)。
每个模块拥有自己的 state、mutation、action、getter、
甚至是嵌套子模块——从上至下进行同样方式的分割
- 对于模块内部的 mutation 和 getter,接收的第一个参数是模块的局部状态对象
- 同样,对于模块内部的 action,局部状态通过 context.state 暴露出来,根节点状态则为 context.rootState
- 对于模块内部的 getter,根节点状态会作为第三个参数暴露出来
5
5
vue_vuex的更多相关文章
随机推荐
- MyBatis # $区别
方式一: <select id="getUserById" resultType="User" parameterType=”int”> SELEC ...
- Newton's Dark Secrets《牛顿探索》
1643年1月4日,在英格兰林肯郡小镇沃尔索浦的一个自耕农家庭里,牛顿诞生了.牛顿是一个早产儿,出生时只有三磅重,接生婆和他的亲人都担心他能否活下来.谁也没有料到这个看起来微不足道的小东西会成为了一位 ...
- Win7 x64位打开VirtualBox报错处理。
错误代码如下: Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but CLSID_VirtualBox w/ IUnknown work ...
- webpack学习笔记——path
__dirname + '/src' path.resolve(__dirname, 'src') path.resolve(__dirname, './src') path.join(__dirna ...
- 计算int数组中的最大,最小,平均值
public static void testNumber(int[] arr) { int max = arr[0]; int min = arr[0]; int avg = 0; int sum ...
- ubuntu 16.04 安装 Mask_RCNN 遇到的问题集锦
源码网页(Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow): https://git ...
- C语言官网蓝桥杯训练1115DNA
这道题是比较简单的输出字符图形的题,但是有几个坑还是要注意下. 1.题中所述的X是大写的,不要看成小写了.(我就错了好几次) 2.每一行输出最后的X后不能在输出空格. 3.输出两个DNA中间有一个空行 ...
- 008_tcp探测
一. 线上出现了监控指标抖动,netscaler心跳监测的问题,如下图: 二.工具如下: 原理参考:https://www.dearcodes.com/index.php/archives/17/ N ...
- nginx——location匹配流程图
location匹配流程图 location理解 1.收到url请求后,nginx首先进行精确匹配(有“=”的为精确匹配),如果匹配成功,则直接返回精确匹配结果,如果没有命中则会继续向下进行普通匹配 ...
- niagara Workbench module import IntelliJ
1. 在整个网络布线中使用一种布线方式,但是两端都有RJ-45 plug 的网络连线,无论是采用方式A还是方式B 端接的方式都是试用的.网络都是通用的,双绞线的顺序与RJ-45偶的引脚序号一一对应, ...