【Vue.js】计算属性和侦听器(五)

2018-08-28  本文已影响0人  kingloongMagic

一、computed:计算属性,根据函数计算值

二、watch:监听属性,监控值改变,改变后执行函数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue学习</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="root">
        姓:<input v-model="firstName"/>
        名:<input v-model="lastName"/>
        <div>{{fullName}}</div>
        <div>{{count}}</div>
    </div>
    <script>
        new Vue({
            el:"#root",
            data:{
                firstName:"",
                lastName:"",
                count:0
            },
            computed:{
                    fullName: function () {
                        return this.firstName + " " + this.lastName
                    },},
            watch:{
                fullName:function () {
                    this.count++
                }
            }
        })
    </script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读