午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

解決Vue 給mapState中定義的屬性賦值報錯的問題

瀏覽:20日期:2023-01-11 17:04:57

1. 實踐環境

Vue 2.9.6

2. 問題描述

<script>import { mapState } from ’vuex’;export default { name: 'displayCount', computed: { ...mapState({ ...略 count: state => state.a.count }) }, methods: { increaseCount () { this.count = this.count + 1 } }};</script><style></style>

如上,我們希望在執行increaseCount函數時,給mapstate函數中映射定義的this.count賦值,給該值增加1,結果,提示

[Vue warn]: Computed property 'count' was assigned to but it has no setter.

3. 解決方案1

如下,把屬性“移出mapState”,然后為屬性新增get,set方法,分別用于獲取值和改變值(按store狀態管理規定的方式)

<script>import { mapState } from ’vuex’;export default { name: 'displayCount', computed: { ...mapState({...略 }), count: { get() { return this.$store.state.a.count; }, set(val) { this.$store.commit('increaseCount', val); } } }, methods: { increaseCount () { this.count = this.count + 1 } }};</script>

注意:this.$store.commit('increaseCount', val);中的increaseCount方法名稱,并不是methods中定義的方法名稱,而是store中定義的方法

4. 解決方案2

通過對比當前屬性值和store狀態值,然后根據比較結果,決定是否根據store狀態管理規則更新狀態值。

<script>import { mapState } from ’vuex’;export default { name: 'displayCount', computed: { ...mapState({ count: state => state.a.count }) }, methods: { increaseCount () { if (this.count == this.$store.state.a.count) { this.$store.commit('increaseCount', this.count+1); } } }};</script>

總結

到此這篇關于解決Vue 給mapState中定義的屬性賦值報錯的問題的文章就介紹到這了,更多相關vue給mapState屬性賦值內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Vue
相關文章:
主站蜘蛛池模板: 在线观看毛片网站 | 午夜久久久久久久 | 久久av一区二区 | 99热偷拍 | 黄色网址国产 | 综合久久网 | 国产a网站 | 成人婷婷 | 黄色大片91| 成人激情视频在线播放 | 久久久精品欧美 | 亚洲精品久久久蜜桃 | 亚洲精品视频在线观看免费 | 黄色裸体视频 | 三级性视频 | 亚洲精品在线视频观看 | 国产做受入口竹菊 | 天天干夜夜操 | 国产精品一二三在线观看 | 香蕉视频在线观看黄 | 欧美三级视频在线 | 激情五月综合网 | 成人免费视频一区二区 | 在线视频 亚洲 | 一级黄在线观看 | 日韩综合久久 | 午夜免费福利 | 影音先锋男人色资源网 | 天天毛片| www.久久久久久久久久 | 日本免费久久 | 69夜色精品国产69乱 | 国产精品视频网址 | 欧美二区在线 | 中文在线免费 | 亚洲欧美福利 | 伊人综合影院 | 精品99久久| av黄色在线播放 | 91免费观看网站 | 久久精选视频 |