詳解Vue computed計算屬性是什么
1. Vue3中的computed函數
1.1. 什么是computed
computed
屬性是Vue3中的一個響應式計算屬性,它可以根據其他響應式數據的變化而自動更新其自身的值。computed
屬性可以接收一個計算函數,并在計算函數中使用其他響應式數據的值進行計算。當任何一個參與計算的響應式數據發(fā)生變化時,computed
屬性會自動重新計算其值,并觸發(fā)相應的依賴更新。
1.2. 如何定義computed
在Vue3中,你可以通過computed
函數來定義一個計算屬性。computed
函數接收一個計算函數作為參數,并返回一個響應式的ref
對象。
下面是一個使用computed
函數形式用法的例子:
import { computed, reactive, ref } from 'vue' const state = reactive({ count: 0 }) const doubleCount = computed(() => { return state.count * 2 }) console.log(doubleCount.value) // 輸出:0 state.count = 1 console.log(doubleCount.value) // 輸出:2
在上面的例子中,我們使用computed
函數定義了一個計算屬性doubleCount
,它的值是state.count
的兩倍。我們可以通過doubleCount.value
來訪問計算屬性的值,并且當state.count
的值發(fā)生變化時,doubleCount
的值也會自動更新。
下面是一個使用computed
對象形式用法的例子:
import { computed, reactive } from 'vue' const state = reactive({ count: 0 }) const doubleCount = computed({ get() { return state.count * 2 }, set(value) { state.count = value / 2 } }) console.log(doubleCount.value) // 輸出:0 doubleCount.value = 6 console.log(state.count) // 輸出:3 console.log(doubleCount.value) // 輸出:6
1.3. computed函數的使用場景
computed
屬性通常用于處理需要根據其他響應式數據計算得出的值的情況。下面是一些computed
屬性的使用場景:
1.3.1. 過濾和排序
當我們需要根據其他響應式數據進行數據過濾和排序時,可以使用computed
屬性來計算得出過濾和排序后的結果。例如:
import { computed, reactive } from 'vue' const state = reactive({ todos: [ { id: 1, text: '學習Vue3', done: false }, { id: 2, text: '學習React', done: false }, { id: 3, text: '學習Angular', done: true } ], filter: 'all' }) const filteredTodos = computed(() => { if (state.filter === 'all') { return state.todos } else if (state.filter === 'active') { return state.todos.filter(todo => !todo.done) } else if (state.filter === 'completed') { return state.todos.filter(todo => todo.done) } }) console.log(filteredTodos.value) // 輸出:[{ id: 1, text: '學習Vue3', done: false }, { id: 2, text: '學習React', done: false }, { id: 3, text: '學習Angular', done: true }] state.filter = 'active' console.log(filteredTodos.value) // 輸出:[{ id: 1, text: '學習Vue3', done: false }, { id: 2, text: '學習React', done: false }]
在上面的例子中,我們使用computed
函數定義了一個計算屬性filteredTodos
,它根據state.todos
和state.filter
的值進行過濾,并返回過濾后的結果。當state.filter
的值發(fā)生變化時,filteredTodos
的值也會自動更新。
1.3.2. 數組計算
當我們需要對一個數組進行計算時,可以使用computed
屬性來計算得出數組的值。例如:
import { computed, reactive } from 'vue' const state = reactive({ todos: [ { id: 1, text: '學習Vue3', done: false }, { id: 2, text: '學習React', done: false }, { id: 3, text: '學習Angular', done: true } ] }) const totalTodos = computed(() => { return state.todos.length }) const completedTodos = computed(() => { return state.todos.filter(todo => todo.done).length }) console.log(totalTodos.value) // 輸出:3 console.log(completedTodos.value) // 輸出:1
在上面的例子中,我們使用computed
函數定義了兩個計算屬性totalTodos
和completedTodos
,它們分別計算了state.todos
數組的總長度和已完成的數量。當state.todos
數組的值發(fā)生變化時,totalTodos
和completedTodos
的值也會自動更新。
2. computed函數的原理
在Vue3中,computed
屬性的原理是使用了一個getter
函數和一個setter
函數來實現。當我們訪問計算屬性的值時,會調用getter
函數進行計算,并將計算結果緩存起來。當參與計算的響應式數據發(fā)生變化時,會觸發(fā)依賴更新,并自動調用getter
函數重新計算計算屬性的值。當我們修改計算屬性的值時,會調用setter
函數進行更新。
總結
computed
屬性是Vue3中的一個響應式計算屬性,它可以根據其他響應式數據的變化而自動更新其自身的值。computed
屬性通常用于處理需要根據其他響應式數據計算得出的值的情況,例如過濾和排序、數組計算等。computed
屬性的原理是使用了一個getter
函數和一個setter
函數來實現,并將計算結果緩存起來,以提高性能和減少計算次數。
到此這篇關于詳解Vue computed計算屬性是什么的文章就介紹到這了,更多相關Vue computed內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue3全局屬性app.config.globalProperties的實現
Vue3中的app.config.globalProperties是一個強大的全局配置功能,允許我們在應用級別設置和訪問屬性,本文主要介紹了Vue3全局屬性app.config.globalProperties的實現,具有一定的參考價值,感興趣的可以了解一下2024-01-01解決vite項目Uncaught Syntaxerror:Unexpected token>vue項
這篇文章主要介紹了解決vite項目Uncaught Syntaxerror:Unexpected token>vue項目上線白屏問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03vue通過v-html指令渲染的富文本無法修改樣式的解決方案
這篇文章主要介紹了vue通過v-html指令渲染的富文本無法修改樣式的解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05