ITPub博客

首页 > IT职业 > IT生活 > vue中computed计算属性与methods对象中的this指针

vue中computed计算属性与methods对象中的this指针

IT生活 作者:darkfiber 时间:2021-09-09 10:27:35 0 删除 编辑

this 指针问题

methods与computed中的this指针 应该指向的是它们自己,可是为什么this指针却可以访问data对象中的成员呢?

因为new Vue对象实例化后data中的成员和computed中的成员为实现化对象属性了,而methods中定义的方法为实现化对象方法了。这时this指针指向的是这个实现化对象。


    let v = new Vue({
        el: '.test',
        data: {
            title: "121213"
        },
        methods: {
            msg() {
                alert(this.title); // ?? this 指针            }
        },
        computed: { 
            prefixTitle() {                return "¥" + this.title;// ?? this 指针            }
        }
    });


图片描述

 

 作者:破壳而出的蝌蚪

原文链接:https://www.cnblogs.com/whnba/p/10503042.html



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4399/viewspace-2822228/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    93
  • 访问量
    527