javascript - 如何在计算中使用其他计算属性

标签 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 it possible to use the computed properties to compute another properties in Vue?这表示这应该是使用其他计算属性的方式。

最佳答案

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

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
    }
}

关于javascript - 如何在计算中使用其他计算属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47403102/

相关文章:

javascript - 使用 useState Hook 在功能组件中使用 Children 来 React Render prop

javascript - 复制/b 除两个之外的所有类型的文件

javascript - jQuery 中的 HTML 字符串返回错误

node.js - 如何使用 Azure AD 验证 VueJS 应用程序?

javascript - 如何在 Vue 3.0 中通过 Webpack 使用 Bundler Build Feature Flags?

vue.js - 从短代码动态注入(inject) Vue 2 组件

javascript - 导入的国家/地区列表为空

javascript - 在谷歌地图中添加新标记之前删除以前的标记

vue.js - 如何使用 vuejs 登陆特定选项卡

javascript - 如何避免在使用 v-for 的元素上使用 v-if