Vuejs - 基于其他计算属性的计算属性 [英] Vuejs - computed property based on other computed property

查看:33
本文介绍了Vuejs - 基于其他计算属性的计算属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个计算属性获取计算属性,如下所示:

I'm trying to get a computed property from another computed property, like so:

var instance = new Vue({
    el: "#instance",
    data: {
        aha: ""
    },
    computed: {
        len: function(){
            return this.aha.length;
        },
        plus : function(){
            return this.len + 2;
        }
    }
});

这不起作用.当我尝试显示 plus 时,我的模板中出现了 NaN.有没有办法使这项工作?这个问题的答案对我不起作用.

This doesn't work. I get NaN in my template when I try to display plus. Is there a way to make this work? The answer to this question doesn't work for me.

推荐答案

您正在尝试访问 number 类型的 length 字段.

You are trying to access the length field of type number.

this.len 是数字,所以 this.len.length 是未定义的.你只需要使用 this.len:

this.len is number, so this.len.length is undefined. you just need to use this.len:

var instance = new Vue({
    el: "#instance",
    data: {
        aha: ""
    },
    computed: {
        len: function(){
            return this.aha.length;
        },
        plus : function(){
            return this.len+ 2;
        }
    }
});

这篇关于Vuejs - 基于其他计算属性的计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆