Vuex 拾遗

引入Vuex的目的:为众多的Vue组件提供一个全局管理共享组件状态的控制中心,当一个共享状态改变时,能使调用该共享状态的组件得到更新。并且使用Vuex的API,每个共享状态的改变都能被追踪。

组件如何引入Vuex:

组件在实例化时,通过store选项引入Vuex的共享变量。之后组件通过this.$store.someprops.subprops访问共享变量

Vuex 通过 store 选项,提供了一种机制将状态从根组件『注入』到每一个子组件中(需调用 Vue.use(Vuex)):

const app = new Vue({
  el: ‘#app‘,
  // 把 store 对象提供给 “store” 选项,这可以把 store 的实例注入所有的子组件
  store,
  components: { Counter },
  template: `
    <div class="app">
      <counter></counter>
    </div>
  `
})

组件的重构为使用Vuex的方式,需要作出的改变:

1、将组件从data获取数据的方式改为计算属性去处理。

computed:{
   shareData(){
       return this.$store.state.shareData
    }
}

Vuex 四大关键词:state,getters,mutations, actions

state:基本的共享状态定义,组件通过this.$score.state.props访问,有简化方法mapState;

getters:适用于对state进行计算,组件通过this.$score.getters.props访问,有简化方法mapGetters;

mutations:用来改变state的状态,由state.commit(‘mutationtype‘)调用,只能用同步方法,有简化方法mapMutations;

actions:解决mutations不能用异步方法的缺陷,其提供了context参数,通过context.commit,context.dispatch触发mutations,或者通过 context.state 和 context.getters 来获取 state 和 getters,有简化方法mapActtions

备注:还有一个modules参数,支持将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割

栗子:

// make sure to call Vue.use(Vuex) if using a module system

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
  	increment: state => state.count++,
    decrement: state => state.count--
  }
})

new Vue({
  el: ‘#app‘,
  computed: {
    count () {
	    return store.state.count
    }
  },
  methods: {
    increment () {
      store.commit(‘increment‘)
    },
    decrement () {
    	store.commit(‘decrement‘)
    }
  }
})

  

时间: 2024-08-03 00:50:16

Vuex 拾遗的相关文章

位运算总结&amp;拾遗

JavaScript 位运算总结&拾遗 最近补充了一些位运算的知识,深感位运算的博大精深,此文作为这个系列的总结篇,在此回顾下所学的位运算知识和应用,同时也补充下前文中没有提到的一些位运算知识. 把一个数变为大于等于该数的最小的2的幂 一个数为2的幂,那么该数的二进制码只有最高位是1. 根据这个性质,我们来举个栗子,比如有数字10,转为二进制码后为: 1 0 1 0 我们只需把 0 bit的位置全部用1填充,然后再把该二进制码加1就ok了.而x | (x + 1)正好可以把最右边的0置为1,可是

C++拾遗--多线程:临界区解决子线程的互斥

C++拾遗--多线程:临界区解决子线程的互斥 前言 为了解决子线程的互斥问题,windows系统提出了关键段或临界区(CRITICAL_SECTION)的概念.它一共有四个共两对操作:初始化.销毁,进入.离开.它们定义在头文件synchapi.h中. 1.初始化变量 VOID WINAPI InitializeCriticalSection( LPCRITICAL_SECTION lpCriticalSection ); 2.销毁变量 VOID WINAPI DeleteCriticalSect

C++拾遗--多线程:原子操作解决线程冲突

C++拾遗--多线程:原子操作解决线程冲突 前言 在多线程中操作全局变量一般都会引起线程冲突,为了解决线程冲突,引入原子操作. 正文 1.线程冲突 #include <stdio.h> #include <stdlib.h> #include <process.h> #include <Windows.h> int g_count = 0; void count(void *p) { Sleep(100); //do some work //每个线程把g_c

C++拾遗--name_cast 显式类型转换

C++拾遗--name_cast 显式类型转换 前言 C++中提供了四种显式的类型转换方法:static_cast,const_cast,reinterpret_cast,dynamic_cast.下面分别看下它们的使用场景. 显式类型转换 1.staitc_cast 这是最常用的,一般都能使用,除了不能转换掉底层const属性. #include <iostream> using namespace std; int main() { cout << "static_c

vuex中filter的使用 &amp;&amp; 快速判断一个数是否在一个数组中

vue中filter的使用 computed: mapState({ items: state => state.items.filter(function (value, index, arr) { return index < 5 }) }), 如上所示,对于vuex,我们在使用mapState获取state时, 可以使用filter来过滤其中的元素,在filter的回调函数中接受三个参数,第一个是value,即每一个元素的值: 第二个是index, 即每一个元素所在的index, 第三个

初识vue 2.0(4):vuex组件通信

0,本来想只用vue就可以做项目了,后来发现不行:一个网页被切分成若干个组件,组件之间是需要数据传递的,因此引入了vuex这个集中式存储.管理的状态管理模式. 1,安装vuex: npm install --save vuex 在main.js中引入: import Vuex from 'vuex' Vue.use(Vuex) 2,创建数据源文件vuex/store.js import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) co

Vue入门之Vuex实战

引言 Vue组件化做的确实非常彻底,它独有的vue单文件组件也是做的非常有特色.组件化的同时带来的是:组件之间的数据共享和通信的难题. 尤其Vue组件设计的就是,父组件通过子组件的prop进行传递数据,而且数据传递是单向的.也就是说:父组件可以把数据传递给子组件,但是 反之则不同.如下图所示: 单向数据流动 单方向的数据流动带来了非常简洁和清晰的数据流,纯展示性或者独立性较强的模块的开发确实非常方便和省事. 但是复杂的页面逻辑,组件之间的数据共享处理就会需要通过事件总线的方式解决或者使用Vue的

[Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js

You'll begin to notice as you build out your actions in Vuex, many of them will look quite similar. Creating a remove action looks almost the same as the add action except for using the axios.delete method then filtering out the deleted todo once the

[Nuxt] Display Vuex Data Differently in Each Page of Nuxt and Vue.js

You often use the same data in different ways across pages. This lesson walks you through setting up multiple pages, retrieving the same data, then displaying it for each page's use-case. index.vue: <template> <div> <form @submit.prevent=&q