VUE中watch的詳細使用教程(推薦!)
1、watch是什么?
watch:是vue中常用的偵聽器(監(jiān)聽器),用來監(jiān)聽數(shù)據(jù)的變化
2、watch的使用方式如下
watch: {
這里寫你在data中定義的變量名或別處方法名: {
handler(數(shù)據(jù)改變后新的值, 數(shù)據(jù)改變之前舊的值) {
這里寫你拿到變化值后的邏輯
}
}
}
3、watch監(jiān)聽簡單案例(監(jiān)聽一個)
<template> <div> <div> <input type="text" v-model="something"> </div> </div> </template> <script> export default { name: "AboutView", components: {}, data() { return { something: "" } }, watch: { //方法1 "something"(newVal, oldVal) { console.log(`新值:${newVal}`); console.log(`舊值:${oldVal}`); console.log("hellow world"); } //方法2 "something": { handler(newVal, oldVal) { console.log(`新的值: ${newVal}`); console.log(`舊的值: ${oldVal}`); console.log("hellow world"); } } } } </script>
在輸入框中輸入1、4 效果圖如下:
4、watch監(jiān)聽復雜單一案例(例:監(jiān)聽對象中的某一項)
<template> <div> <div> <input type="text" v-model="obj.something"> </div> </div> </template> <script> export default { name: "AboutView", components: {}, data() { return { obj: { something: "" } } }, watch: { "obj.something": { handler(newVal, oldVal) { console.log(`新的值: ${newVal}`); console.log(`舊的值: ${oldVal}`); console.log("hellow world"); } } } } </script>
在輸入框中輸入4、5 效果圖如下:
5、watch中immediate的用法和作用
1、作用:immediate頁面進來就會立即執(zhí)行,值需要設為true
2、用法如下方代碼所示:
<template> <div> <div> <input type="text" v-model="obj.something"> </div> </div> </template> <script> export default { name: "AboutView", components: {}, data() { return { obj: { something: "" } } }, watch: { "obj.something": { handler(newVal, oldVal) { console.log(`新的值: ${newVal}`); console.log(`舊的值: ${oldVal}`); console.log("hellow world"); }, immediate:true } } } </script>
進來頁面后立即加載,效果圖如下:
6、watch中deep 深度監(jiān)聽的用法和作用
1、作用:deep 用來監(jiān)聽data中的對象,值需要設為true
2、用法如下方代碼所示:
<template> <div> <div> <input type="text" v-model="obj.something"> </div> </div> </template> <script> export default { name: "AboutView", components: {}, data() { return { obj: { something: "" } } }, watch: { "obj": { handler(newVal, oldVal) { console.log(`新的值: ${newVal}`); console.log(`舊的值: ${oldVal}`); console.log("hellow world"); }, deep:true } } } </script>
注意:
1、console.log(`新的值: ${newVal}`); 這種打印出來的是對象的類型,如下圖:
2、console.log(newVal);這種打印出來的是對象本身,如下圖:
總結(jié)
到此這篇關(guān)于VUE中watch的詳細使用教程的文章就介紹到這了,更多相關(guān)VUE watch使用教程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3+TS+Vant3+Pinia(H5端)配置教程詳解
這篇文章主要介紹了Vue3+TS+Vant3+Pinia(H5端)配置教程詳解,需要的朋友可以參考下2023-01-01解決在Vue中使用axios POST請求變成OPTIONS的問題
這篇文章主要介紹了解決在Vue中使用axios POST請求變成OPTIONS的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08解決router.beforeEach()動態(tài)加載路由出現(xiàn)死循環(huán)問題
這篇文章主要介紹了解決router.beforeEach()動態(tài)加載路由出現(xiàn)死循環(huán)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10Element?UI中v-infinite-scroll無限滾動組件使用詳解
在移動端數(shù)據(jù)的更新中許多方法孕育而生,無限滾輪也是解決的方案一種,Element-ui為vue開發(fā)了一個事件(v-infinite-scroll),下面這篇文章主要給大家介紹了關(guān)于Element?UI中v-infinite-scroll無限滾動組件使用的相關(guān)資料,需要的朋友可以參考下2023-02-02使用Vue CLI創(chuàng)建typescript項目的方法
這篇文章主要介紹了使用Vue CLI創(chuàng)建typescript項目的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08Vue2.0 實現(xiàn)歌手列表滾動及右側(cè)快速入口功能
這篇文章主要介紹了Vue2.0實現(xiàn)歌手列表滾動及右側(cè)快速入口功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08