vuex分模塊后,實現(xiàn)獲取state的值
問題:vuex分模塊后,一個模塊如何拿到其他模塊的state值,調(diào)其他模塊的方法?
思路:
1.通過命名空間取值--this.$store.state.car.list // OK
2.通過定義該屬性的getter方法,因方法全局注冊,不存在命名空間,可以通過this直接調(diào)用。
this.$store.state.car.carGetter
我在car模塊中自己的定義了state, getters,
this.$store.state.car.list可以拿到值.
但是,this.$store.state.car.carGetter報錯,
請問.如何在組件中調(diào)用這個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 ?
已解決!
模塊內(nèi)部的 action、mutation、和 getter 現(xiàn)在仍然注冊在全局命名空間——這樣保證了多個模塊能夠響應(yīng)同一 mutation 或 action。
補充知識:vuex使用模塊的時候 獲取state里的數(shù)據(jù)語法
普通語法
this.$store.state.【哪個數(shù)據(jù)】
模塊化語法:
this.$store.state.【哪個模塊】.【哪個數(shù)據(jù)】
<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">立即結(jié)算({{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分模塊后,實現(xiàn)獲取state的值就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+element使用動態(tài)加載路由方式實現(xiàn)三級菜單頁面顯示的操作
這篇文章主要介紹了vue+element使用動態(tài)加載路由方式實現(xiàn)三級菜單頁面顯示的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
詳解vue中的父子傳值雙向綁定及數(shù)據(jù)更新問題
這篇文章主要介紹了vue中的父子傳值雙向綁定及數(shù)據(jù)更新問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
解決Mint-ui 框架Popup和Datetime Picker組件滾動穿透的問題
這篇文章主要介紹了解決Mint-ui 框架Popup和Datetime Picker組件滾動穿透的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
html頁面引入vue組件之http-vue-loader.js解讀
這篇文章主要介紹了html頁面引入vue組件之http-vue-loader.js解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

