前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vuex mapGetters「建议收藏」

vuex mapGetters「建议收藏」

作者头像
全栈程序员站长
发布2022-06-27 08:47:15
3300
发布2022-06-27 08:47:15
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

1、vuex 配置

代码语言:javascript
复制
//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使用

代码语言:javascript
复制
<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>

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133575.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年6月8,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档