126. python高级------vue(6)

126. python高级------vue(6)

python修炼第二十九天

2019年 4月 29日 晴

监听属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./vue.min.js"></script>
    <script>
        window.onload = function () { 

            new Vue({
                el: "#app",
                data: {
                    num: 0
                },
                methods: {
                    func1: function () { 
                        this.num += 1
                     }
                },
                watch: {  // 用于设置要监听的属性
                    num: function () {   // 当num属性发生变化时, 会调用该匿名函数
                        
                        // vue属性变化时, 会先触发监听, 再更新界面
                        if (this.num > 9){
                            this.num = 111
                        }
                     }
                },
            })
         }
    </script>
</head>
<body>
    <div id="app">
        {{num}}<br/>
        <button @click="func1">按钮</button>
    </div>
</body>
</html>

版权声明:本文为qq_40455733原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。