vue基礎之監聽器



categories:

  • vue基礎
    tags:
  • 監聽器

目錄

監聽器

watch 監聽的是單個屬性基本的數據類型
基本數據類型 簡單監視
復雜數據類型 深度監視

<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue基礎之監聽器</title>
</head>
<body>
<div id="app">
    <input type="text" v-model="msg">
    <p>{{msg}}</p>
    <button @click="stus[0].name='rose'">改變</button>
    <h4>{{stus[0].name}}</h4>
</div>
<script src="./vue.js"></script>
<script>

    new Vue({
        el:'#app',
        data(){
            return {
                msg:'',
                stus:[{
                    name:'jack'
                }]
            }
        },
        // watch 監聽的是單個屬性基本的數據類型
        // 基本數據類型 簡單監視
        // 復雜數據類型 深度監視
        watch:{
            // 監聽字符串
            msg:function(newV,oldV1){
                console.log(newV,oldV1);
                if (newV == 'tusir'){
                    console.log("tusir來了")
                }
            },
        //    監聽復雜數據類型 object array 嘗試監視
            stus:{
                deep:true,
                handler:function (newV,oldV) {
                    console.log(newV[0].name,oldV[0].name,)
                }
            }
        }
    })
</script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM