Vue的watch和computed方法的使用及區(qū)別介紹
Vue的watch屬性
Vue的watch屬性可以用來監(jiān)聽data屬性中數(shù)據(jù)的變化
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="lib/vue.min.js"></script> <script src="lib/vue-router-3.0.1.js"></script> </head> <body> <div id="app"> <input type="text" v-model="firstname" /> </div> <script type="text/javascript"> var vm = new Vue({ el:"#app", data:{ firstname:"", lastname:"" }, methods:{}, watch:{ firstname:function(){ console.log(this.firstname) } } }) </script> </body> </html>
可以從上述代碼中實踐得知,輸入框內(nèi)的值變化多少次,控制臺就會打印多少次
同時還可以直接在監(jiān)聽的function中使用參數(shù)來獲取新值與舊值
watch:{ firstname:function(newValue,OldValue){ console.log(newValue); console.log(OldValue); } }
其中第一個參數(shù)是新值,第二個參數(shù)是舊值
同時Watch還可以被用來監(jiān)聽路由router的變化,只是這里的監(jiān)聽的元素是固定的
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="lib/vue.min.js"></script> <script src="lib/vue-router-3.0.1.js"></script> <style type="text/css"> </style> </head> <body> <div id="app"> <!-- 由于Vue-router的hash匹配原則所以我們需要在原定義的路徑上加一個#號 --> <!-- <a href="#/login" rel="external nofollow" >登錄</a> <a href="#/register" rel="external nofollow" >注冊</a>--> <router-link to="/login" tag="span">登錄</router-link> <router-link to="/register">注冊</router-link> <router-view></router-view> </div> </body> <script> var login={ template:'<h1>登錄組件</h1>' } var register={ template:'<h1>注冊組件</h1>' } var routerObj = new VueRouter({ routes:[ //此處的component只能使用組件對象,而不能使用注冊的模板的名稱 {path:"/login",component:login}, {path:"/register",component:register} ] }) var vm = new Vue({ el:'#app', data:{ }, methods:{ }, router:routerObj,//將路由規(guī)則對象注冊到VM實例上 watch:{ '$route.path':function(newValue,OldValue){ console.log(newValue); console.log(OldValue); } } }) </script> </html>
計算屬性Computed的作用
computed屬性的作用與watch類似,也可以監(jiān)聽屬性的變化
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="lib/vue.min.js"></script> <script src="lib/vue-router-3.0.1.js"></script> </head> <body> <div id="app"> <input type="text" v-model="firstname" /> <input type="text" v-model="lastname" /> <input type="text" v-model="fullname" /> </div> <script type="text/javascript"> var vm = new Vue({ el:"#app", data:{ firstname:"", lastname:"" }, methods:{}, /* watch:{ firstname:function(newValue,OldValue){ console.log(newValue); console.log(OldValue); } }*/ computed:{ fullname:function(){ return this.firstname +"-"+this.lastname } } }) </script> </body> </html>
只是他會根據(jù)他依賴的屬性,生成一個屬性,讓vm對象可以使用這個屬性
methods,watch,computed的區(qū)別
- computed 屬性的結果會被緩存,除非依賴的響應式屬性變化才會重新計算。主要當作屬性來使用;
- methods 方法表示一個具體的操作,主要書寫業(yè)務邏輯;
- watch 一個對象,鍵是需要觀察的表達式,值是對應回調(diào)函數(shù)。主要用來監(jiān)聽某些特定數(shù)據(jù)的變化,從而進行某些具體的業(yè)務邏輯操作;可以看作是 computed 和 methods 的結合體;
總結
以上所述是小編給大家介紹的Vue的watch和computed方法的使用及區(qū)別介紹,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
vue框架編輯接口頁面下拉級聯(lián)選擇并綁定接口所屬模塊
這篇文章主要為大家介紹了vue框架編輯接口頁面實現(xiàn)下拉級聯(lián)選擇以及綁定接口所屬模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05vue3的watch用法以及和vue2中watch的區(qū)別
這篇文章主要介紹了vue3的watch用法以及和vue2中watch的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04