【问题标题】:Is it possible to render elements conditionally in NativeScript-Vue?是否可以在 NativeScript-Vue 中有条件地渲染元素?
【发布时间】:2020-01-15 08:35:38
【问题描述】:

我希望你能帮助我解决我的问题。 我正在开发一个 Nativescript-Vue 应用程序,我想有条件地渲染它:

<Label if="isRendering" text="This is a text"></Label>

但这不起作用。

我已经用&lt;v-template if="..."&gt;&lt;/v-template&gt; 试过了,但这也不起作用。

如果我没记错的话,在 Vue.js 中可以使用 v-if 进行条件渲染,我正在寻找在 Nativescript-Vue 中进行渲染的可能性。

谢谢

【问题讨论】:

    标签: vue.js nativescript-vue


    【解决方案1】:

    和 Vue.js 中的一样。

    您应该使用v-if,而不是使用if

    Playground example

    例子:

    <Label v-if="isRendering" text="This is a text"></Label>
    

    但如果你想在ListView 中使用if,那就不同了。 示例:

    <ListView for="item in listOfItems" @itemTap="onItemTap"> 
      <v-template>
        <Label :text="item.text" /> 
      </v-template>
    
      <v-template if="$odd">
        <!-- For items with an odd index, shows the label in red. -->
        <Label :text="item.text" color="red" />
      </v-template>
    </ListView>
    

    但更多信息请参见docs

    【讨论】:

    • 非常感谢您的回答。我已经尝试过像这样使用: ... 但它什么也没做,它总是渲染元素。我不想使用 ListView,但我认为我必须使用“if”
    • @Razzaui 您应该在示例中使用v-if 而不是if。检查 Playground 示例。我为你做的,所以你可以测试它。还有什么问题可以问。