【发布时间】: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 事件。
我已经尝试解决这个问题好几个小时了,但一无所获。谢谢!
【问题讨论】:
-
您的计算取决于
allTimes。您正在通过this.allTimes[index] = time;对其进行修改。但是你不能这样做,如果你按索引修改数组,vue 反应性会中断:vuejs.org/2016/02/06/common-gotchas/… -
非常感谢!
标签: javascript vue.js computed-properties