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

Posted whnba

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue中computed计算属性与methods对象中的this指针相关的知识,希望对你有一定的参考价值。

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 指针
            }
        }
    });

技术图片

 

以上是关于vue中computed计算属性与methods对象中的this指针的主要内容,如果未能解决你的问题,请参考以下文章

Vue--computed计算属性监听数据变化----与watch,methods对比

vue之computed(计算属性)的使用方法有哪些?

Vue中的methods属性和computed属性的区别

vue实例的computed属性

vue-computed计算属性

Vue的计算属性