網站首頁 Vue 正文
簡介
說明
本文用示例介紹Vuex的五大核心之一:getters。
官網
getters概述
說明
getters 是Store的計算屬性,可以對State進行計算操作。就像計算屬性一樣,getter 的返回值會根據它的依賴被緩存起來,且只有當它的依賴值發生了改變才會被重新計算。
雖然組件內也可以做計算屬性,但getters 可以在多組件之間復用。如果一個狀態只在一個組件內使用,可以不用getters。
來源
有時我們需要從 store 中的 state 中派生出一些狀態,例如對列表進行過濾并計數:
computed: { doneTodosCount () { return this.$store.state.todos.filter(todo => todo.done).length } }
如果有多個組件需要用到此屬性,我們要么復制這個函數,或者抽取到一個共享函數然后在多處導入它(無論哪種方式都不是很理想)。getter就是為了解決這個問題而產生的。
用法
直接使用
this.$store.getters.計算屬性名 // 不分模塊 this.$store.getters['模塊名/計算屬性名'] // 分模塊
mapGetters
import { mapGetters } from 'vuex' export default { computed: { // 不分模塊 ...mapGetters(['計算屬性名']) // 分模塊,不重命名計算屬性 ...mapGetters('模塊名', ['計算屬性名']) // 分模塊,重命名計算屬性 ...mapGetters('模塊名', {'新計算屬性名': '舊計算屬性名'}) } }
示例
代碼
CouterStore.js
import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const counterStore = new Vuex.Store( { state: { count: 10 }, getters: { doubleCount(state) { return state.count * 2; } } } ); export default counterStore;
Parent.vue
<template> <div class="outer"> <h3>父組件</h3> <component-b></component-b> </div> </template> <script> import ComponentB from "./ComponentB"; export default { name: 'Parent', components: {ComponentB}, } </script> <style scoped> .outer { margin: 20px; border: 2px solid red; padding: 20px; } </style>
ComponentB.vue
<template> <div class="container"> <h3>ComponentB</h3> <div>計數器的值:{{thisCount}}</div> <div>計數器的2倍:{{thisDoubleCount}}</div> </div> </template> <script> export default { computed:{ thisCount() { return this.$store.state.count; }, thisDoubleCount() { return this.$store.getters.doubleCount; }, } } </script> <style scoped> .container { margin: 20px; border: 2px solid blue; padding: 20px; } </style>
路由(router/index.js)
import Vue from 'vue' import Router from 'vue-router' import Parent from "../components/Parent"; Vue.use(Router) export default new Router({ routes: [ { path: '/parent', name: 'Parent', component: Parent, } ], })
測試
訪問:http://localhost:8080/#/parent
原文鏈接:https://blog.csdn.net/feiying0canglang/article/details/122724236
相關推薦
- 2022-05-25 Flutter?StaggeredGridView實現瀑布流效果_Android
- 2022-12-05 一文深入了解Python中的繼承知識點_python
- 2022-08-15 接口狀態與策略路由表
- 2023-05-15 golang判斷結構體為空的問題_Golang
- 2022-07-18 Android 防重復點擊(Kotlin 協程實現 和 Handler實現)
- 2022-03-14 Failed to load ApplicationContext
- 2022-01-17 如何實現 input 和 textarea 自動聚焦
- 2023-04-10 pytest?用例執行失敗后其他不再執行_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支