vue2中watch的用法(通俗易懂,簡(jiǎn)單明了)
一,監(jiān)聽(tīng)基本普通屬性:
字符串,布爾值,number
(1)把要監(jiān)聽(tīng)的msg值看作方法名,來(lái)進(jìn)行監(jiān)聽(tīng)。
<template> <div id="app"> <div> <button @click="btnClick">觸發(fā)</button> <div>{{ msg }}</div> </div> </div> </template> <script> export default { data() { return { msg: "codekey", } }, methods: { btnClick() { this.msg = 'pink' } }, watch: { // watch第一次綁定值的時(shí)候不會(huì)執(zhí)行監(jiān)聽(tīng),修改數(shù)據(jù)才會(huì)觸發(fā)函數(shù) msg(newVal,oldVal) { console.log('oldVal:',oldVal) // coderkey console.log('newVal:',newVal) // pink } } }; </script>
(2)把要監(jiān)聽(tīng)的msg值看作對(duì)象,利用hanler方法來(lái)進(jìn)行監(jiān)聽(tīng)
watch第一次綁定值的時(shí)候不會(huì)執(zhí)行監(jiān)聽(tīng),如果需要第一次就執(zhí)行監(jiān)聽(tīng) 則需要設(shè)置: immediate: true
<template> <div id="app"> <div> <button @click="btnClick">觸發(fā)</button> <div>{{ msg }}</div> </div> </div> </template> <script> export default { data() { return { msg: "codekey", } }, methods: { btnClick() { this.msg = 'pink' } }, watch: { // 如果需要第一次就執(zhí)行監(jiān)聽(tīng) 則需要設(shè)置:immediate: true msg: { handler(newVal,oldVal) { console.log('oldVal:',oldVal) console.log('newVal:',newVal) }, immediate: true }, } }; </script>
二,監(jiān)聽(tīng)對(duì)象:
(1)監(jiān)聽(tīng)對(duì)象需要用到深度監(jiān)聽(tīng),設(shè)置 deep:true
<template> <div id="app"> <div> <button @click="btnClick">觸發(fā)</button> <div>{{ obj.name }}</div> </div> </div> </template> <script> export default { data() { return { obj: { name: 'coderkey', age: 18 }, } }, methods: { btnClick() { this.obj.name = 'pink' } }, watch: { obj:{ // 注意:屬性值發(fā)生變化后,handler執(zhí)行后獲取的 newVal 值和 oldVal 值是一樣的 handler(newVal,oldVal) { console.log('oldVal:',oldVal) console.log('newVal:',newVal) }, immediate: true, // 開(kāi)啟深度監(jiān)聽(tīng) deep deep: true } } }; </script>
(2)可以只監(jiān)聽(tīng)對(duì)象的其中一個(gè)屬性值 ’對(duì)象.屬性‘ 的形式
<template> <div id="app"> <div> <button @click="btnClick">觸發(fā)</button> <div>{{ obj.name }}</div> </div> </div> </template> <script> export default { data() { return { obj: { name: 'coderkey', age: 18 }, } }, methods: { btnClick() { this.obj.name = 'pink' } }, watch: { // 函數(shù)執(zhí)行后,獲取的 newVal 值和 oldVal 值不一樣 'obj.name'(newVal,oldVal) { console.log('oldVal:',oldVal) // coderkey console.log('newVal:',newVal) // pink }, /* 'obj.name':{ handler(newVal,oldVal) { console.log('oldVal:',oldVal) console.log('newVal:',newVal) }, // 立即處理 進(jìn)入頁(yè)面就觸發(fā) immediate: true } */ } } </script>
三,監(jiān)聽(tīng)數(shù)組
(1)(一維、多維)數(shù)組的變化不需要深度監(jiān)聽(tīng)
<template> <div id="app"> <div> <button @click="btnClick">觸發(fā)</button> <!-- <div>{{ obj.name }}</div> --> </div> </div> </template> <script> export default { data() { return { arr1:[1,2,4,5,6] } }, methods: { btnClick() { this.arr1.unshift(0) }, }, watch: { arr1:{ handler(newVal,oldVal) { console.log('oldVal:',oldVal) // [0,1,2,4,5,6] console.log('newVal:',newVal) // [0,1,2,4,5,6] }, // immediate: true, } } } </script>
多維數(shù)組
<template> <div id="app"> <div> <button @click="btnClick">觸發(fā)</button> <!-- <div>{{ obj.name }}</div> --> </div> </div> </template> <script> export default { data() { return { arr1:[1,2,4,[5,6]] } }, methods: { btnClick() { this.arr1.push(0) }, }, watch: { arr1:{ handler(newVal,oldVal) { console.log('oldVal:',oldVal) console.log('newVal:',newVal) }, // immediate: true, } } } </script>
(2)數(shù)組對(duì)象中對(duì)象屬性變化監(jiān)測(cè)需要使用 deep:true
深度監(jiān)聽(tīng),多少層內(nèi)產(chǎn)生變化都可以監(jiān)測(cè)到。
<template> <div id="app"> <div> <button @click="btnClick">觸發(fā)</button> <!-- <div>{{ obj.name }}</div> --> </div> </div> </template> <script> export default { data() { return { arr1: [ { name: 'coderkey', age: 22 } ], arr2: [ { name: 'coderkey', age: 22, children: [ { name: 'why', sex: 33 } ] } ] } }, methods: { btnClick() { this.arr1[0].name = 'pink' this.arr2[0].children[0].name = 'pink' }, }, watch: { arr1: { handler(newVal, oldVal) { console.log('newVal1', newVal); console.log('oldVal1', oldVal); }, deep: true }, arr2: { handler(newVal, oldVal) { console.log('newVal2', newVal); console.log('oldVal2', oldVal); }, deep: true } } } </script>
四,監(jiān)聽(tīng)路由變化
// 方法一: watch:{ $router(to,from){ console.log(to.path) } } // 方法二: watch: { $route: { handler: function (val, oldVal){ console.log(val); }, // 深度觀察監(jiān)聽(tīng) deep: true } }, // 方法三: watch: { '$route' : 'getRoutePath' }, methods: { getRoutePath(){ console.log( this .$route.path); } }
總結(jié)
到此這篇關(guān)于vue2中watch的用法的文章就介紹到這了,更多相關(guān)vue2中watch用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談vue的props,data,computed變化對(duì)組件更新的影響
本篇文章主要介紹了淺談vue的props,data,computed變化對(duì)組件更新的影響,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01vue之監(jiān)聽(tīng)頁(yè)面是否以到底部
這篇文章主要介紹了vue之監(jiān)聽(tīng)頁(yè)面是否以到底部問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Vue3使用vant檢索組件van-search遇到的問(wèn)題小結(jié)
當(dāng)清空按鈕與檢索按鈕同時(shí)居右時(shí),點(diǎn)擊clear清空按鈕事件時(shí)會(huì)同時(shí)觸發(fā)click-right-icon事件,這個(gè)時(shí)候容易觸發(fā)一系列問(wèn)題,小編小編給大家分享Vue3使用vant檢索組件van-search遇到的問(wèn)題小結(jié),感興趣的朋友一起看看吧2024-02-02element-ui Upload上傳組件動(dòng)態(tài)配置action方式
這篇文章主要介紹了element-ui Upload上傳組件動(dòng)態(tài)配置action方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07element-ui?table表格控件實(shí)現(xiàn)單選功能代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于element-ui?table表格控件實(shí)現(xiàn)單選功能的相關(guān)資料,單選框是指在?Element?UI?的表格組件中,可以通過(guò)單選框來(lái)選擇一行數(shù)據(jù)。用戶只能選擇一行數(shù)據(jù),而不能同時(shí)選擇多行,需要的朋友可以參考下2023-09-09elementui之el-table如何通過(guò)v-if控制按鈕顯示與隱藏
這篇文章主要介紹了elementui之el-table如何通過(guò)v-if控制按鈕顯示與隱藏問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11