例子:

index.js

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import mutations from './mutations'
  4. import actions from './action'
  5. import getters from './getters'
  6.  
  7. Vue.use(Vuex)
  8.  
  9. const state = {
  10. userInfo: { phone: 111 }, //用户信息
  11. orderList: [{ orderno: '1111' }], //订单列表
  12. orderDetail: null, //订单产品详情
  13. login: false, //是否登录
  14. }
  15.  
  16. export default new Vuex.Store({
  17. state,
  18. getters,
  19. actions,
  20. mutations,
  21. })
  1. computed: {
  2. ...mapState([
  3. 'orderList',
  4. 'login'
  5. ]),
  6. },
  7. mounted(){
  8. console.log(typeof orderList); ==>undefind
  9. console.log(typeof this.orderList)==>object
  10. }

mapState通过扩展运算符将store.state.orderList 映射this.orderList  这个this 很重要,这个映射直接映射到当前Vue的this对象上。

所以通过this都能将这些对象点出来,同理,mapActions, mapMutations都是一样的道理。牢记~~~

  1. import Vuex from 'vuex'
  2. import Vue from 'vue'
  3. Vue.use(Vuex)
  4.  
  5. export default new Vuex.Store({
  6. state:{
  7. data:'test'
  8. },
  9. getters:{
  10.  
  11. },
  12. mutations:{
  13.  
  14. },
  15. actions:{
  16.  
  17. }
  18. })
  1. <template>
  2. <div id="app">
  3. {{count}}
       //{{data}}
  4. </div>
  5. </template>
  6. <script>
  7. //想要使用 首先需要按需引入
  8. import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'
  9. export default {
  10. // 通过对象展开运算符将count混入computed对象中,作为computed对象的方法使用
  11. computed:{
  12. //相当于
  13. // count(){
  14. // return this.$store.state.data
  15. // }
      
      //采用对象方式相当于重命名
  16. ...mapState({
  17. count: 'data'
  18. })
      //采用数组方式
      //...mapState([data])
      //可在其他钩子中使用this.data调用  
  19. }
  20. //其他mapGetters,mapMutations,mapActions原理一样
    }
    </script>
    <style>
  21.  
  22. </style>

另外mapState通过扩展运算符将store.state.data映射this.count  这个this 很重要,这个映射直接映射到当前Vue的this对象上。

在钩子函数中可直接 this.count调用

当state,mutations,actions数量很多时,在一个文件夹下不方便管理。可把state,getters,mutations,actions分别写在不同文件内,通过export 导出。再在一个文件中引入。

例如:

  1. //state.js
  2. const state={
  3. count:10
  4. }
  5. export default state; //切记记得导出
  1. //getters.js
  2. export const gets= state => state.count
    //这里直接导出 就不用export default
    相当于 gets=function(state){
                 return state.count
               }
  1. //mutations.js
  2. const mutations={
  3. add(state){
  4. state.count+=1
  5. },
  6. sub(state){
  7. state.count-=1
  8. }
  9. }
  10.  
  11. export default mutations;
  1. //actions.js
  2. export const addactions=(context)=>{
  3. context.commit('add')
  4. }
  5. export const subactions=(context)=>{
  6. context.commit('sub')
  7. }

所有文件导出后在一个文件内统一引入

  1. //store文件夹下的store.js
    import Vue from 'vue'
  2. import Vuex from 'vuex'
  3.  
  4. import state from './state.js'
    //由于上边的getters.js 和actions.js是通过export 导出每一个,
  5. import * as getters from './getters.js'
  6. import mutations from './mutations.js'
    import * as actions from './actions.js'
  7.  
  8. Vue.use(Vuex)
  9.  
  10. export default new Vuex.Store({
  11. state,
  12. getters,
  13. mutations,
  14. actions
  15. })

使用

  1. <template>
  2. <div id="app">
  3. <div>数量{{count}}</div>
  4. <div @click="add">增加</div>
  5. <div @click="sub">减少</div>
  6. </div>
  7. </template>
  8.  
  9. <script>
  10. import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'
  11.  
  12. export default {
  13. computed:{
      //注意这里可以直接使用,因为通过...mapxxx引进来,因为是state,所以可以直接当对象的属性使用,若是mutations,actions则当对象的方法使用。
  14. ...mapState(["count"])
  15.  
  16. },
  17. methods:{
  18. ...mapMutations(["add","sub"])
  19. }
  20. }
  21. </script>

vuex mapState、mapGetters、mapActions、mapMutations的使用的更多相关文章

  1. vuex之 mapState, mapGetters, mapActions, mapMutations 的使用

    一.介绍 vuex里面的四大金刚:State, Mutations,Actions,Getters (上次记得关于vuex笔记 http://www.cnblogs.com/adouwt/p/8283 ...

  2. vuex中的辅助函数 mapState,mapGetters, mapActions, mapMutations

    1.导入辅助函数 导入mapState可以调用vuex中state的数据 导入mapMutations可以调用vuex中mutations的方法 四个辅助函数 各自对应自己在vuex上的自己 2.ma ...

  3. Vue 状态管理之vuex && {mapState,mapGetters}

    1 # 一.理解vuex 2 1.概念:专门在Vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中式的管理(读写),也是一种组件间通信的方式,且适用于任意组件间 ...

  4. vuex里mapState,mapGetters使用详解

    这次给大家带来vuex里mapState,mapGetters使用详解,vuex里mapState,mapGetters使用的注意事项有哪些,下面就是实战案例,一起来看一下. 一.介绍 vuex里面的 ...

  5. vuex 的使用 mapState, mapGetters, mapMutations, mapActions

    state => 基本数据getters => 从基本数据派生的数据mutations => 提交更改数据的方法,同步!actions => 像一个装饰器,包裹mutation ...

  6. Vuex mapGetters,mapActions

    一.基本用法 1. 初始化并创建一个项目 ? 1 2 3 vue init webpack-simple vuex-demo cd vuex-demo npm install 2. 安装 vuex ? ...

  7. 理解Vuex的辅助函数mapState, mapActions, mapMutations用法

    在讲解这些属性之前,假如我们项目的目录的结构如下: ### 目录结构如下: demo1 # 工程名 | |--- dist # 打包后生成的目录文件 | |--- node_modules # 所有的 ...

  8. [转]理解Vuex的辅助函数mapState, mapActions, mapMutations用法

    原文地址:https://www.cnblogs.com/tugenhua0707/p/9794423.html 在讲解这些属性之前,假如我们项目的目录的结构如下: ### 目录结构如下: demo1 ...

  9. mapState ,mapGetters ,mapMutations,mapActions

    参考 http://www.imooc.com/article/14741

随机推荐

  1. Docker Data Center系列(三)- DTR安装指南

    本系列文章演示如何搭建一个mini的云平台和DevOps实践环境. 基于这套实践环境,可以部署微服务架构的应用栈,演练提升DevOps实践能力. 1 系统要求 1.1 硬件和软件要求 成为UCP管理的 ...

  2. java----八种排序算法

    1.直接插入排序 经常碰到这样一类排序问题:把新的数据插入到已经排好的数据列中. 将第一个数和第二个数排序,然后构成一个有序序列 将第三个数插入进去,构成一个新的有序序列. 对第四个数.第五个数……直 ...

  3. 解决tomcat中文传输乱码问题

    <Connector URIEncoding="utf-8" connectionTimeout="20000" encoding="utf-8 ...

  4. EOS智能合约开发(二):EOS创建和管理钱包

    上节介绍了EOS智能合约开发之EOS环境搭建及启动节点 那么,节点启动后我们要做的第一件事儿是什么呢?就是我们首先要有账号,但是有账号的前提是什么呢?倒不是先创建账号,而是先要有自己的一组私钥,有了私 ...

  5. Android 5.0 版本 USB 调试模式打开方法

    Android 4.2 版本 USB 调试模式打开方法 1. 进入“设置”页面,点击“关于平板电脑”.见下图红色方框.   2. 疯狂点击“版本号”,见下图红色方框,直到出现“您现在处于开发者模式!” ...

  6. mysql Client does not support authentication protocol requested by server; consider upgrading MySQL

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

  7. MC9SD64单片机快速入门 I/O寄存器

    I/O的使用 数据方向寄存器和数据寄存器的配置 I/O输入输出的使用: 数据方向寄存器与数据寄存器 寄存器的概念: 寄存器,是集成电路中非常重要的一种存储单元,通常由触发器组成.在集成电路设计中,寄存 ...

  8. react-native 简介及环境

    概要 react native 环境搭建 hello react native react native 发布 react native https://facebook.github.io/reac ...

  9. 推荐几本FPGA书籍(更新中)

    1.<数字信号处理的FPGA实现>第三版 讲解比较详细的DSP理论,使用FPGA实现,不过使用VHDL语言:也颇具参考性. 2. <Xilinx Zynq-7000 嵌入式系统设计与 ...

  10. aliyun mysql

    https://segmentfault.com/q/1010000009603559?sort=created