1、vuex 配置

//vuex的配置
//注意Store是大写
const store = new Vuex.Store({
//数据保存
state: {
show: false,
count: 0,
list: [1, 5, 8, 10, 30, 50]
},
mutations: {
increase(state, n = 1) {
state.count += n;
},
decrease(state, n = 1) {
state.count -= n;
},
switch_dialog (state) { // 这里的state对应着上面这个state
state.show = state.show ? false : true
// 你还可以在这里执行其他的操作改变state } },
getters: {
filteredList: state
=> {
return state.list.filter(item => item < 31
);
}
},

actions:{
asyncIncrease(context){
context.commit('increase');
},
switch_dialog123 (context) { // 这里的context和我们使用的$store拥有相同的对象和方法
context.commit('switch_dialog')
// 你还可以在这里触发其他的mutations方法 }
}
});

2、mapGetters使用

<template>
<div>
{{count}}
<button @click="handleIncrease">+5</button>
<button @click="handleAsyncIncrease">-5</button> {{filteredList}}
<button @click="handleRouter">跳转到 HelloWorld3</button>
<button @click="showRouter">展示路由</button>
</div>
</template> <script>
import { mapState } from 'vuex'
import { mapGetters } from 'vuex'

export default {
name: 'HelloWorld2',
computed: {
// count(){
// return this.$store.state.count;
// },
// filteredList() {
// return this.$store.getters.filteredList;
// },

...mapState({
count: state => state.count
}),
// 使用对象展开运算符将 getter 混入 computed 对象中
...mapGetters([
'filteredList'

])

},
methods: {
handleIncrease() {
this.$store.commit('increase', 5);
},
handleDecrease() {
this.$store.commit('decrease', 5);
},
handleAsyncIncrease() {
this.$store.dispatch('asyncIncrease');
},
handleRouter() {
this.$router.push('/HelloWorld3');
},
showRouter() {
console.log(this.$router);
console.log(this.$router.push);
}
}
};
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped> </style>

vuex mapGetters的更多相关文章

  1. Vuex mapGetters,mapActions

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

  2. 解决 vuex mapGetters 语法报错 (Unexpected token )

    在使用vuex2的mapGetters 和 mapActions 的方法时,借助 stage2 的 Object Rest Operator 特性,可以写出下面代码:  computed: { ... ...

  3. vuex的简单例子和vue model组件

    好久没用过vuex了,vuex官方示例的计算器counter是用的webpack打包了单文件组件,不方便回顾,今天把代码改成了html引人的方式,方便回顾 <!DOCTYPE html> ...

  4. Vue躬行记(9)——Vuex

    Vuex是一个专为Vue.js设计的状态管理库,适用于多组件共享状态的场景.Vuex能集中式的存储和维护所有组件的状态,并提供相关规则保证状态的独立性.正确性和可预测性,这不仅让调试变得可追踪,还让代 ...

  5. vuex 源码分析(六) 辅助函数 详解

    对于state.getter.mutation.action来说,如果每次使用的时候都用this.$store.state.this.$store.getter等引用,会比较麻烦,代码也重复和冗余,我 ...

  6. Vue学习日记(四)——Vue状态管理vuex

    前言 先说句前话,如果不是接触大型项目,不需要有多个子页面,不使用vuex也是完全可以的. 说实在话,我在阅读vuex文档的时候,也很难以去理解vuex,甚至觉得没有使用它我也可以.但是直到我在项目碰 ...

  7. vuex辅助函数和vuex5个属性

    在上篇中,我们可以知道如果想要访问vuex.store中state中的数据,需要this.$store.state.属性名.显然这样访问数据写的代码很很不简洁的,辅助函数就是用来解决这个问题的. 1. ...

  8. 逐行粒度的vuex源码分析

    vuex源码分析 了解vuex 什么是vuex vuex是一个为vue进行统一状态管理的状态管理器,主要分为state, getters, mutations, actions几个部分,vue组件基于 ...

  9. Vue-admin工作整理(九): 状态管理Vuex-state和getters

    前提:通过项目结构个人设置创建的项目 store文件下actions.js.mutations.js.state.js都是根级别的状态管理,引用入口是通过index.js来实现,整个Vuex处理逻辑为 ...

随机推荐

  1. [HEOI2014][bzoj3611] 大工程 [虚树+dp]

    题面: 传送门 思路: 又是一道虚树入门级的题目,但是这道题的实际难点在于dp 首先,这道题是可以点分治做的,而且因为6s时限随便浪,所以写点分治也不是不可以 但是,dp因为$O\left(n\rig ...

  2. 觉醒力量 (hidpower)

    觉醒力量 (hidpower) 题目描述 [题目背景] 从前有一款非常火的游戏被人们称为pokemon,从最初的红绿蓝黄版直到现在的XY版,都受到世界各地小朋友和大朋友们的喜爱. [题意描述] 作为一 ...

  3. iOS-app发布证书和调试证书配置

    iOS-app发布证书.真机调试证书.测试证书.推送证书详细过程 更重要的是让你彻底明白为什么要这样配置证书 说句废话:凡事当你弄清楚为什么时,就揭开了它复杂和神秘的面纱 正文开始 一:发布证书 遵旨 ...

  4. 使用 swagger组件给asp.net webapi文档生成

    1.名词解释 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模 ...

  5. nginx进行项目域名配置时提示Job for nginx.service failed

    ps aux | grep nginx /bin/systemctl stop nginx.service /bin/systemctl start nginx.service /bin/system ...

  6. bugs view:

    Expecially those business bugs! I should check better especially when data changes! This place requi ...

  7. css3 实现多行文本折行

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. [LeetCode] Factorial Trailing Zeroes 阶乘末尾0

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  9. Caps_Locl exchang Esc

    vim ~/.Xmodmap  1 remove Lock = Caps_Lock                                          2 keysym Escape = ...

  10. 关于URL编码 [转]

    转自: http://www.ruanyifeng.com/blog/2010/02/url_encoding.html 作者: 阮一峰 日期: 2010年2月11日 一.问题的由来 URL就是网址, ...