Vue學習(8)監視器

watch

簡單監視 單個屬性 簡單數據結構

深度監視 複雜屬性 複雜數據結構

<!--
 * @Author: your name
 * @Date: 2020-04-01 16:50:28
 * @LastEditTime: 2020-04-01 17:11:06
 * @LastEditors: Please set LastEditors
 * @Description: 監聽(內存地址監聽)
 * @FilePath: \x\11.html
 -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <input type="text" v-model="msg"/>
        <button @click="stus[0].name='rose'">change</button>
        <h3>{{ msg }}</h3>
        <h4>{{ stus[0].name }}</h4>
    </div>
    <script type="text/javascript" src="./node_modules/vue/dist/vue.js"></script>
    <script type="text/javascript">
        // 總結:watch監聽單個屬性,基本數據類型,簡單監視
        // 複雜的數據類型,深度監視
        new Vue({
            el: "#app",
            data() {
                return {
                    msg: '',
                    stus: [{name: 'jack'}]
                }
            },
            watch: {
                msg: function(newV, oldV) {
                    console.log(newV, oldV);
                    if (newV == 'k') {
                        console.log('lai');
                    }
                },
                // 監聽複雜數據類型,深度監視
                stus: {
                    deep: true, // 深度監視
                    handler: function(newV, oldV) {
                        console.log(newV[0].name);
                    }
                }
            },
        })
    </script>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章