vuex分模組後,實現獲取state的值
阿新 • • 發佈:2020-07-26
問題: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',components: { CartListItem },computed: { CartList () { return this.$store.state.cartModule.updateCartList },...mapGetters(['cartQuantity','cartTotal']) } } </script>
以上這篇vuex分模組後,實現獲取state的值就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。