【问题标题】:Computed property not updating计算属性未更新
【发布时间】:2019-04-18 16:01:38
【问题描述】:

我有一个奇怪的问题。我正在尝试做一个简单的时间跟踪应用程序。

所以我有这个表格。

<form class="form" @submit.prevent="saveHours">
    <div class="status">
        <div class="selector" v-for="(select, index) in select_all">
            <component :is="select" :id="index" @percentage="trackTime"></component>
        </div>
    </div><!-- /.status -->
    <div class="form-submit">
        <button type="submit" class="form__submit">
            <span v-if="loading">Guardando...</span>
            <span v-else>Guardar</span>
        </button>
    </div>
</form>

这是我的 vue 代码

export default {
    name: 'home',
    data() {
        return {
            select_all: [Selector],
            loading: false,
            allTimes: [],
            saveForm: []
        }
    },
    components: {
        Selector
    },
    computed: {
        calculateTotal() {
            return this.allTimes.reduce((accumulator, currentValue) => parseInt(accumulator) + parseInt(currentValue), 0);
        }
    },
    methods: {
        addNewSelector() {
            this.calcTotal();
            this.select_all.push(Selector)
        },
        trackTime(time, index, proyecto) {
            this.currentTime = time;
            this.allTimes[index] = time;

            const data = {
                time,
                proyecto
            }

            this.saveForm[index] = data;
        },
        saveHours() {
            const currentWeek = moment(new Date()).format('w');
            const diverRef = db.collection('divers').doc(firebaseAuth.currentUser.email);
            const currentWeekRef = diverRef.collection('reportes').doc(`semana_${currentWeek}`);
            var self = this;
            currentWeekRef.get().then(function(doc) {
                if ( doc.exists ) {
                    console.log('Ya registraste tus horas');
                } else {
                    currentWeekRef.set({
                        data: self.saveForm
                    })
                }
            });
        },
    }
}

我有一个组件叫做.

我正在尝试使用计算属性 calculateTotal 将时间相加,以便我知道用户何时完成了 100% 的时间。但是没有更新。

真的很奇怪,如果我将它用作一种方法,它就像一个魅力,但它对我不起作用,因为我希望它在用户输入时更新,因为我正在使用一个输入组件我不能使用keyup 事件。

我已经尝试解决这个问题好几个小时了,但一无所获。谢谢!

【问题讨论】:

标签: javascript vue.js computed-properties


【解决方案1】:

如果有人遇到同样的问题,Sergeon 和 Roy J 哪里对。

我正在这样做

this.allTimes[index] = time;

这搞砸了 Vue。我将代码更改为

this.allTimes.splice(index, 1, time)

它现在完美运行。

谢谢

【讨论】:

    猜你喜欢
    • 2020-08-11
    • 2014-03-01
    • 2019-10-03
    • 2017-07-29
    • 2016-03-07
    • 1970-01-01
    • 2019-06-18
    • 2015-11-16
    相关资源
    最近更新 更多