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

Why can't I use constant number instead of 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 它给出了 INVALID ARRAY LENGTH 错误。

提前感谢您的回复。


请检查 {N} Playground 中的代码

Playground of "modify counts (item.number - 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)