我找不到一种方法来选择不同的选项来渲染 v-for 中的文本。是否有可能或者我是否需要以不同的方式构建逻辑来执行类似于下面的代码的操作?
<template>
<ul v-show="showNotifications">
<li v-for="notification in notifications">
// if notification.type = 'friend request'
New friend request from {{ notification.name }}
// else
New notification from {{ notification.name }}
</li>
</ul>
</template>
通知是一个包含名称和通知类型等数据的对象数组。
最佳答案
使用另一个 template
元素如下(未测试)
<template>
<ul v-show="showNotifications">
<li v-for="notification in notifications">
<template v-if="notification.type == 'friend request'">
New friend request from {{ notification.name }}
</template>
<template v-else>
New notification from {{ notification.name }}
</template>
</li>
</ul>
关于vuejs2 - v-for 内部的 v-if 和 v-else 用于不同的文本渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42634908/