vuejs2 - v-for 内部的 v-if 和 v-else 用于不同的文本渲染

标签 vuejs2

我找不到一种方法来选择不同的选项来渲染 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/

相关文章:

javascript - 在模板 Vue 中插入一个脚本标签

javascript - Vue `$refs` 问题

node.js - 204 错误代码然后 500 错误代码响应

javascript - 如何在 SweetAlert2 html 输出中使用 v-for

Vue.js - 选择/下拉所选项目 vm 绑定(bind)不起作用 (bootstrap-vue)

vue.js - 是否可以将数据从一个 vue 应用程序推送到另一个应用程序?

vue.js - VueJS 和动态标题

javascript - 在 v-for 列表 : VueJS 中显示元素

javascript - vue-select 根据ip地址选择选项

javascript - vue-cli 中的 react 性属性错误