vue中的計算屬性和偵聽屬性
計算屬性
計算屬性用于處理復(fù)雜的業(yè)務(wù)邏輯
計算屬性具有依賴性,計算屬性依賴 data中的初始值,只有當(dāng)初始值改變的時候,計算屬性才會再次計算
計算屬性一般書寫為一個函數(shù),返回了一個值,這個值具有依賴性,只有依賴的那個值發(fā)生改變,他才會重新計算
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <meta http-equiv='X-UA-Compatible' content='ie=edge'> <title>表單輸入綁定</title></head><body> <div id='app'> {{ reverseMsg }}---- {{ reverseMsg }}-------- {{ reverseMsg }} //直接使用計算屬性中的函數(shù)就是所要的數(shù)據(jù) </div></body><script src='http://www.lshqa.cn/bcjs/vue.js'></script> //vue的js,不然使用不了vue語法<script> const app = new Vue({ el: ’#app’, data: { msg: ’hello world’ }, computed: { reverseMsg () { // 計算屬性是一個函數(shù),返回一個值,使用和data中的選項(xiàng)一樣 console.log(’computed’) // 執(zhí)行1次 --- 依賴性 return this.msg.split(’’).reverse().join(’’); } } })</script></html>
偵聽屬性(監(jiān)聽屬性)
vue提供了檢測數(shù)據(jù)變化的一個屬性 watch 可以通過 newVal 獲取變化之后的值
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <meta http-equiv='X-UA-Compatible' content='ie=edge'> <title>表單輸入綁定</title></head><body> <div id='app'> <input type='text' v-model='firstname'> + <input type='text' v-model='lastname'> = {{ fullname }} </div></body><script src='http://www.lshqa.cn/bcjs/vue.js'></script><script> const app = new Vue({ el: ’#app’, data: { firstname: ’李’, lastname: ’小龍’, fullname: ’李小龍’ }, watch: { // 偵聽屬性 firstname (newVal, oldVal) { // newVal變化之后的值 this.fullname = newVal + this.lastname // 當(dāng)改變 姓 的時候執(zhí)行 }, lastname (newVal, oldVal) { this.fullname = this.firstname + newVal // 當(dāng)改變 名字 的時候執(zhí)行 } } })</script></html>
以上就是vue中的計算屬性和偵聽屬性的詳細(xì)內(nèi)容,更多關(guān)于vue 計算屬性和偵聽屬性的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. jsp實(shí)現(xiàn)登錄驗(yàn)證的過濾器2. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))3. css代碼優(yōu)化的12個技巧4. phpstudy apache開啟ssi使用詳解5. jsp EL表達(dá)式詳解6. 解析原生JS getComputedStyle7. xpath簡介_動力節(jié)點(diǎn)Java學(xué)院整理8. 輕松學(xué)習(xí)XML教程9. jsp cookie+session實(shí)現(xiàn)簡易自動登錄10. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法
