You need to enable JavaScript to run this app. Vue中如何基于条件为每个对象渲染子组件(无法同时使用v-for和v-if)
优惠活动
大模型
产品
解决方案
定价
更多

Vue中如何基于条件为每个对象渲染子组件(无法同时使用v-for和v-if)

Vue实现方案

Vue禁止在同一个元素上同时使用v-for和v-if,你可以通过以下两种方式实现和React代码等价的效果:

方式一:使用<template>占位标签实现

<template>是Vue的内置占位符,不会生成真实DOM节点,你可以在<template>上绑定v-for做列表遍历,内部实际要渲染的组件上绑定v-if做条件判断,逻辑和你React的写法几乎完全对齐:

<template>
  <svg :viewBox="`0 0 ${bottomRight[0]} ${bottomRight[1]}`">
    <!-- 渲染边 -->
    <template v-for="(edgeData, edgeKey) in edges" :key="edgeKey">
      <DirectedEdge
        v-if="
          time >= edgeData.timeRange[0] &&
          time <= edgeData.timeRange[1] &&
          vertices[JSON.parse(edgeKey)[0]]?.coord &&
          vertices[JSON.parse(edgeKey)[1]]?.coord
        "
        :start="vertices[JSON.parse(edgeKey)[0]].coord"
        :end="vertices[JSON.parse(edgeKey)[1]].coord"
        :label="edgeData.label"
      />
    </template>
    <!-- 渲染顶点 -->
    <template v-for="(verticeData, verticeKey) in vertices" :key="verticeKey">
      <Vertice
        v-if="time >= verticeData.times[0]"
        :center="verticeData.coord"
        :label="verticeData.label"
      />
    </template>
  </svg>
</template>

<script setup lang="ts">
import DirectedEdge from './directed-edge.vue'
import Vertice from './vertice.vue'

// 声明入参类型
defineProps<{
  time: number
  vertices: Record<string, {
    coord: [number, number]
    label: string
    memoized: boolean
    times: number[]
  }>
  edges: Record<string, {
    label: number
    timeRange: [number, number]
  }>
  bottomRight: [number, number]
}>()
</script>

<style scoped>
svg {
  /* 对应你原来Svg组件的样式定义 */
  width: 100%;
  height: 100%;
}
</style>

方式二:使用计算属性预过滤数据(更推荐,性能更好)

如果你的edges和vertices数据量较大,建议先通过计算属性把符合渲染条件的项过滤好,再直接在模板中遍历,避免模板中重复做数据解析和条件判断,运行效率更高:
仅需要在上面的<script setup>中新增两个计算属性:

import { computed } from 'vue'

const props = defineProps(/* 此处省略和上面一致的入参定义 */)

// 预过滤需要渲染的边
const renderedEdges = computed(() => {
  return Object.entries(props.edges).filter(([key, data]) => {
    const [u, v] = JSON.parse(key)
    return props.time >= data.timeRange[0]
      && props.time <= data.timeRange[1]
      && props.vertices[u]?.coord
      && props.vertices[v]?.coord
  }).map(([key, data]) => {
    const [u, v] = JSON.parse(key)
    return {
      key,
      start: props.vertices[u].coord,
      end: props.vertices[v].coord,
      label: data.label
    }
  })
})

// 预过滤需要渲染的顶点
const renderedVertices = computed(() => {
  return Object.entries(props.vertices).filter(([_, data]) => {
    return props.time >= data.times[0]
  }).map(([key, data]) => {
    return {
      key,
      center: data.coord,
      label: data.label
    }
  })
})

对应的模板可以简化为:

<template>
  <svg :viewBox="`0 0 ${bottomRight[0]} ${bottomRight[1]}`">
    <DirectedEdge
      v-for="edge in renderedEdges"
      :key="edge.key"
      :start="edge.start"
      :end="edge.end"
      :label="edge.label"
    />
    <Vertice
      v-for="vertice in renderedVertices"
      :key="vertice.key"
      :center="vertice.center"
      :label="vertice.label"
    />
  </svg>
</template>

内容的提问来源于stack exchange,提问作者djangodev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 02:21:00