vue中监听属性

 监听属性可以针对某个属性进行监听,当监听的属性的值发生了变化,则会执行相应的函数。
 监听的函数要写在vue的watch属性中。
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <title>vue中监听属性</title>
</head>

<body>
    <div id="app">
        <div>
            <label>查询:</label>
            <input type="text" v-model:value="keyword">
        </div>
        <span>结果:{{ret}}</span>
    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                keyword: '',
                ret: '',
            },
            watch: {
                keyword(searchWord, retWord) {
                    this.ret = '正在加载...'
                    setTimeout(()=>{
                        this.ret = searchWord
                    },1000);
                }
            }
        })
    </script>
</body>

</html>

 

posted @ 2020-02-20 14:56  xsan  阅读(2057)  评论(0编辑  收藏  举报