首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vue js计算方法

vue js计算方法
EN

Stack Overflow用户
提问于 2017-09-10 14:29:54
回答 1查看 1.9K关注 0票数 0

我对Vue和Js非常陌生,我对计算方法有点困惑。因此,如下所示,我创建了一个道具来共享来自父组件的数据。所有操作都正常,但是当sumTotal方法作为默认值执行时,它在{{sumTotal}}上显示Nan。我想知道如何将int 0呈现为sumTotal值的默认值。

代码语言:javascript
复制
     //parent component
     <my-colors :myProp="selectedShape.price"></my-colors>

</template>

<script>

import Colors from './Colors.vue';

export default {


    data() {

        return {
            selectedShape: {},
            shapes: [{
                id: 1,
                name: "Square",
                price: 4,

            }, {
                id: 2,
                name: "Circle",
                price: 6,

            }]
        }
    },

    computed: {

        totalShape: function() {
            var totalShape = 0;
            for (var i = 0; i < this.shapes.length; i++) {
                if (this.shapes[i].selected) {
                    totalShape += this.shapes[i].price;
                }
            }
            return totalShape;
        }
    },
    methods: {
        getSelectedShape() {
                return this.selectedShape;

            },
    }
}

</script>



      //child component

    <v-layout row v-for="color in colors" :key="color.id">
            <v-layout >
                <v-flex >
                    <v-checkbox class="text-xs-right" name="checkbox"  v-bind:label="`${color.name}`" v-model="color.checked" light></v-checkbox>
                </v-flex>
            </v-layout>
            <v-layout column>
                <v-flex >
                    <v-subheader>{{color.price}} €</v-subheader>
                </v-flex>
            </v-layout>
                    <v-subheader> {{sumTotal}} €</v-subheader>
    </v-layout>    
            <script>

            export default {

                props: ['myProp'],

                data: () => ({
                    colors: [{
                        id: 1,
                        name: "White",
                        price: 5,
                    }, {
                        id: 2,
                        name: "Green",
                        price: 4,
                    }, {
                        id: 3,
                        name: "Blue",
                        price: 3,
                    }, {
                        id: 4,
                        name: "Red",
                        price: 2,
                    }, {
                        id: 5,
                        name: "Purple",
                        price: 1,
                    }, {
                        id: 6,
                        name: "Yellow",
                        price: 0,
                    }],
                }),

                computed: {

                    total: function() {
                        var total = 0;
                        for (var i = 0; i < this.colors.length; i++) {
                            if (this.colors[i].checked) {
                                total += this.colors[i].price;
                            }

                        }
                        return total;
                    },

                    sumTotal: function() {
                      var myProp = 0;
                      return this.total + this.myProp;
                    }
                },
            }

            </script>
EN

回答 1

Stack Overflow用户

发布于 2017-09-23 20:51:54

当您的子组件第一次呈现时,父组件的数据属性selectedShape等于{},因此传递给子组件的selectedShape.price属性将是undefined,当您调用sumTotal方法时,它返回someNumber + undefined,即NaN

要解决这个问题,应该将默认的price值设置为selectedShape属性:

代码语言:javascript
复制
selectedShape: { price: 0}

或者你可以改变你的sumTotal

代码语言:javascript
复制
sumTotal: function() {
  return this.total + (this.myProp || 0);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46141810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档