如何在计算

时间:2017-11-20 23:56:44

标签: javascript vue.js vuejs2

我正在尝试这个:

    props: ["gameState"],
    computed: {
        cards() {
            return this.gameState.playerCards
        },
        rows() {
            let cards = this.cards();
            let max = 6;
            if (cards.length <= max)
                return [cards]

            var mid = Math.ceil(cards.length / 2);
            let return_value = [cards.slice(0, mid), cards.slice(mid)]
            return return_value
        }
    }

但它告诉我this.cards is not a function。我在看Is it possible to use the computed properties to compute another properties in Vue?,它说这应该是使用其他计算属性的方法。

1 个答案:

答案 0 :(得分:1)

这就是我在评论中解释的内容。

computed: {
    cards() {
        return this.gameState.playerCards
    },
    rows() {
        let cards = this.cards;
        // if cards is ever undefined (maybe its populated asynchronously), 
        // cards.length will blow up. So, check here and return a sane
        // value when it's undefined            
        if (!cards) return []

        let max = 6;
        if (cards.length <= max)
            return [cards]

        var mid = Math.ceil(cards.length / 2);
        let return_value = [cards.slice(0, mid), cards.slice(mid)]
        return return_value
    }
}
相关问题