为什么我不能使用常量数字而不是 this.item.number?



<template>
<Page>
<ActionBar title="item" />
<ScrollView>
<StackLayout>
<Label textWrap="true" v-for="n in times" :text="n" />
</StackLayout>
</ScrollView>
</Page>
</template>
<script>
export default {
props: ["item"],
data() {
return {
times: this.item.subTotal - this.item.subtrackfromTotal // OK
// times: 9 - 5, // OK
// times: this.item.subTotal - 5 //error: INVALID ARRAY LENGTH
};
}
};
</script>

我想从(我的数据的数字字段(中减去5并 => 在v-for="n in times"中使用它 但是当我使用诸如5 之类的常量时,它会给出无效的数组长度错误。

为什么times: this.item.subTotal - 5会失败?

请帮我弄清楚;如何在我的数据和常数上使用运算符,同时让 vue 相信我发送的是一个常数而不是数组?

当我尝试times: this.item.subTotal - this.item.subtrackfromTotaltimes: 9 - 5Vue 接受时间作为常数时。 但是当我尝试times: this.item.subTotal - 5时,它给出了无效的数组长度错误。

感谢您提前回复。


请在 {n} 操场中查看代码

"修改计数(项目编号 - 5("的游乐场

您收到该错误是因为该值为负数。

对于其中一个项目,this.item.subTotal的值为 4,因此times为 -1。

new Vue({
el: '#app',

data () {
return {
times: -1
}
}
})
<script src="https://unpkg.com/vue@2.6.11/dist/vue.js"></script>
<div id="app">
<div v-for="a in times"></div>
</div>

您如何修复它取决于您在这种情况下希望的行为是什么。也许这个?

times: Math.max(0, this.item.subTotal - 5)

相关内容

最新更新