为了账号安全,请及时绑定邮箱和手机立即绑定

Vue的watch和computed属性

标签:
Vue.js

Vue的watch属性

Vue的watch属性可以用来监听data属性中数据的变化

<!DOCTYPE html><html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="lib/vue.min.js"></script>
        <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="lib/vue-router-3.0.1.js"></script>
    </head>
    <body>
        <div id="app">
            <input type="text" v-model="firstname" />
        </div>
        <script type="text/javascript">
            var vm = new Vue({                el:"#app",                data:{                    firstname:"",                    lastname:""
                },                methods:{},                watch:{                    firstname:function(){                        console.log(this.firstname)
                    }
                }
            })        </script>
    </body></html>

可以从上述代码中实践得知,输入框内的值变化多少次,控制台就会打印多少次

同时还可以直接在监听的function中使用参数来获取新值与旧值

                watch:{                    firstname:function(newValue,OldValue){                        console.log(newValue);                        console.log(OldValue);
                    }
                }

其中第一个参数是新值,第二个参数是旧值

同时Watch还可以被用来监听路由router的变化,只是这里的监听的元素是固定的

<!DOCTYPE html><html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="lib/vue.min.js"></script>
        <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="lib/vue-router-3.0.1.js"></script>
        <style type="text/css">
        </style>
    </head>
    <body>
        
        <div id="app">
            <!--
                由于Vue-router的hash匹配原则所以我们需要在原定义的路径上加一个#号
            --><!--            <a href="#/login">登录</a>
            <a href="#/register">注册</a>-->
            <router-link to="/login" tag="span">登录</router-link>
            <router-link to="/register">注册</router-link>
            <router-view></router-view>
        </div>
    </body>
    <script>
        var login={            template:'<h1>登录组件</h1>'
        }        var register={            template:'<h1>注册组件</h1>'
        }        var routerObj = new VueRouter({            routes:[            //此处的component只能使用组件对象,而不能使用注册的模板的名称
                {path:"/login",component:login},
                {path:"/register",component:register}
            ]
        })        var vm = new Vue({            el:'#app',            data:{
            },            methods:{
                
            },            router:routerObj,//将路由规则对象注册到VM实例上
            watch:{                '$route.path':function(newValue,OldValue){                        console.log(newValue);                        console.log(OldValue);
                }
            }
        })    </script></html>

计算属性Computed的作用

computed属性的作用与watch类似,也可以监听属性的变化

<!DOCTYPE html><html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="lib/vue.min.js"></script>
        <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="lib/vue-router-3.0.1.js"></script>
    </head>
    <body>
        <div id="app">
            <input type="text" v-model="firstname" />
            <input type="text" v-model="lastname" />
            <input type="text" v-model="fullname" />
        </div>
        <script type="text/javascript">
            var vm = new Vue({
                el:"#app",
                data:{
                    firstname:"",
                    lastname:""
                },
                methods:{},/*              watch:{
                    firstname:function(newValue,OldValue){
                        console.log(newValue);
                        console.log(OldValue);
                    }
                }*/
                computed:{
                    fullname:function(){                        return this.firstname +"-"+this.lastname
                    }
                }
            })        </script>
    </body></html>

只是他会根据他依赖的属性,生成一个属性,让vm对象可以使用这个属性

methods,watch,computed的区别

  1. computed属性的结果会被缓存,除非依赖的响应式属性变化才会重新计算。主要当作属性来使用;

  2. methods方法表示一个具体的操作,主要书写业务逻辑;

  3. watch一个对象,键是需要观察的表达式,值是对应回调函数。主要用来监听某些特定数据的变化,从而进行某些具体的业务逻辑操作;可以看作是computedmethods的结合体;



作者:镇屌要逆袭
链接:https://www.jianshu.com/p/3337e6fd5291


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消