您好,登錄后才能下訂單哦!
watch單個監聽
computed多個監聽
當watch監聽復雜數據類型的時候需要做深度監聽deep
watch深度監聽deep表達式:
// 深度監聽
watch:{
msg:{
handler(val, oldval){
if(val.text == 'love'){
alert('I Love You')
}
},
deep:true//開啟深度監聽
}
}
watch深度監聽
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript" src="vue.js"></script>
<div id="app">
<div>watch監聽器</div>
<input type="text" v-model="msg.text">
</div>
<script type="text/javascript">
new Vue({
el:'#app',
data(){
return {
msg:{text:''},
}
},
// 深度監聽
watch:{
msg:{
handler(val, oldval){
if(val.text == 'love'){
alert('I Love You')
}
},
deep:true//開啟深度監聽
}
}
})
</script>
</body>
</html>
watch監聽
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript" src="vue.js"></script>
<div id="app">
<div>watch監聽器</div>
<input type="text" v-model="msg">
</div>
<script type="text/javascript">
new Vue({
el: '#app',
data() {
return {
msg: ''
}
},
// 深度監聽
watch: {
msg: {
handler(val, oldval) {
if (val == 'love') {
alert('I Love You')
}
}
}
}
})
</script>
</body>
</html>
computed多監聽
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript" src="vue.js"></script>
<div id="app">
<div>computed監聽</div>
<br>
(<input type="text" v-model="n1">+
<input type="text" v-model="n2">)*
<input type="text" v-model="n3">={{result}}
</div>
<script type="text/javascript">
new Vue({
el:'#app',
data(){
return {
n1:'',
n2:'',
n3:''
}
},
// computed監聽多個
computed:{
result(){
return (Number(this.n1)+Number(this.n2))*Number(this.n3)
}
}
})
</script>
</body>
</html>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。