vuex分模块后,实现获取state的值
网络编程 2021-07-04 14:06www.168986.cn编程入门
这篇文章主要介绍了vuex分模块后,实现获取state的值,具有很好的参考价值,希望对大家有所帮助。一起跟随长沙网络推广过来看看吧
问题:vuex分模块后,一个模块如何拿到其他模块的state值,调其他模块的方法?
思路:
1.通过命名空间取值--this.$store.state.car.list // OK
2.通过定义该属性的getter方法,因方法全局注册,不存在命名空间,可以通过this直接调用。
this.$store.state.car.carGetter
我在car模块中自己的定义了state, getters,
this.$store.state.car.list可以拿到值.
但是,this.$store.state.car.carGetter报错,
请问.如何在组件中调用这个getters,
//car.js state = { list: [] } getters = { carGetter: state => { return state.list.filter(''); } } new Vuex.Store({ getters: { test: state => { return '02'; } }, modules: { car } }) // 组件 this.$store.state.car.list // OK this.$store.state.car.carGetter // undefined this.$store.state.carGetter // 为什么这么用ok, 难道会把模块中的getters注册到root ?
已解决!
模块内部的 action、mutation、和 getter 现在仍然注册在全局命名空间——这样保证了多个模块能够响应同一 mutation 或 action。
补充知识:vuex使用模块的时候 获取state里的数据语法
普通语法
this.$store.state.【哪个数据】
模块化语法:
this.$store.state.【哪个模块】.【哪个数据】
<template> <div class="panel panel-info"> <div class="panel-heading"> <h4 class="panel-title">购物车列表</h4> </div> <div class="panel-body"> <p v-if="!CartList.length">这里什么都没有,请先添加商品。</p> <CartListItem v-for="ele in CartList" :key="ele.id" :cartlist-iteam="ele"/> </div> <div class="panel-footer"> <a href="" class="btn btn-block btn-danger">清空购物车({{cartQuantity}})</a> <a href="" class="btn btn-block btn-info">立即结算({{cartTotal}})</a> </div> </div> </template> <script> import CartListItem from './CartListItem' import { mapGetters } from 'vuex' export default { name: 'CartList', ponents: { CartListItem }, puted: { CartList () { return this.$store.state.cartModule.updateCartList }, ...mapGetters(['cartQuantity', 'cartTotal']) } } </script>
以上这篇vuex分模块后,实现获取state的值就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程