例子:

index.js

import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations'
import actions from './action'
import getters from './getters' Vue.use(Vuex) const state = {
userInfo: { phone: 111 }, //用户信息
orderList: [{ orderno: '1111' }], //订单列表
orderDetail: null, //订单产品详情
login: false, //是否登录
} export default new Vuex.Store({
state,
getters,
actions,
mutations,
})
computed: {
...mapState([
'orderList',
'login'
]),
},
mounted(){
console.log(typeof orderList); ==>undefind
console.log(typeof this.orderList)==>object
}

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

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

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

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

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

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

例如:

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

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

//store文件夹下的store.js
import Vue from 'vue'
import Vuex from 'vuex' import state from './state.js'
//由于上边的getters.js 和actions.js是通过export 导出每一个,
import * as getters from './getters.js'
import mutations from './mutations.js'
import * as actions from './actions.js' Vue.use(Vuex) export default new Vuex.Store({
state,
getters,
mutations,
actions
})

使用

<template>
<div id="app">
<div>数量{{count}}</div>
<div @click="add">增加</div>
<div @click="sub">减少</div>
</div>
</template> <script>
import {mapState,mapGetters,mapMutations,mapActions} from 'vuex' export default {
computed:{
  //注意这里可以直接使用,因为通过...mapxxx引进来,因为是state,所以可以直接当对象的属性使用,若是mutations,actions则当对象的方法使用。
...mapState(["count"]) },
methods:{
...mapMutations(["add","sub"])
}
}
</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. Linux中DNS的设置

    1.查看本机的域名 hostname 2.修改DNS 临时修改: hostname desktop0.example.com 永久修改: hostnamectl set-hostname deskto ...

  2. java传输文件的简单方法

    假设现在已经打包了一个文件(1233444333),要将这个文件传输给另一方: package file; import java.io.*; public class F_PasswordUnPas ...

  3. java中带图片按钮的大小设置

    在java部分需要用到图形界面编程的项目中,经常会使用图片设置对按钮进行美化,但是使用时会出现一个很麻烦的问题,那就是按钮的大小默认按照图片的大小来显示,这大大降低了界面的美观程度: 按照方法: JB ...

  4. ORACLE等待事件:SQL*Net message from client & SQL*Net message to client

    在ORACLE当中有两个很常见的等待事件"SQL*Net message from client"与"SQL*Net message to client",两者 ...

  5. [20180927]ora-01426.txt

    [20180927]ora-01426.txt --//链接:http://www.itpub.net/thread-2105458-1-1.html1.环境:SCOTT@test01p> @ ...

  6. docker:版本变更

    在2017年之前的版本号: v1.4,  v1.5,  v1.6,  v1.7, v1.8, v1.9, v1.10, v1.11, v1.12, v1.13 从2017年开始版本后变更为:${yy} ...

  7. python3编写网络爬虫18-代理池的维护

    一.代理池的维护 上面我们利用代理可以解决目标网站封IP的问题 在网上有大量公开的免费代理 或者我们也可以购买付费的代理IP但是无论是免费的还是付费的,都不能保证都是可用的 因为可能此IP被其他人使用 ...

  8. Go学习笔记05-指针

    目录 参数传递 var a int = 2 var pa *int = &a *pa = 3 fmt.Println(a) Go语言中 指针不能运算 参数传递 不像C++.Java.Pytho ...

  9. MyBatis动态SQL之一使用 if 标签和 choose标签

    bootstrap react https://segmentfault.com/a/1190000010383464 xml 中 < 转义 to thi tha <if test=&qu ...

  10. ab和jmeter进行GET/POST压力测试的使用心得和比较

    引言: 互联网服务压测是非常重要的评价方法,ab,webbench,jmeter等都是业界流行的测试工具,ab和webbench作为shell模式下轻量级的测试工具,jmeter则作为有GUI界面的更 ...