Vue 条件渲染动态切换组件案例解析

作者:犯困乐 发布时间: 2026-05-30 阅读量:10 评论数:0

案例概述

利用条件渲染指令(v-ifv-else-ifv-else)实现三个组件的动态切换效果。

组件结构

src/components/2026530/2/
├── ComponentA.vue           # 组件 A
├── ComponentB.vue           # 组件 B
├── ComponentC.vue           # 组件 C
└── ConditionalRendering.vue  # 主组件(条件渲染)

核心概念

条件渲染指令

指令 说明
v-if 条件为真时渲染元素
v-else-if 前一个条件不满足,当前条件满足时渲染
v-else 所有条件都不满足时渲染

语法结构

<ComponentA v-if="condition === 'A'" />
<ComponentB v-else-if="condition === 'B'" />
<ComponentC v-else />

代码实现详解

1. ComponentA.vue

<template>
  <div class="component-a">
    <h2>组件 A</h2>
    <p>这是第一个组件的内容</p>
  </div>
</template>

<style scoped>
.component-a {
  padding: 20px;
  background-color: #e3f2fd;
  border: 2px solid #2196f3;
  border-radius: 8px;
  margin: 10px;
}
</style>

蓝色主题的组件。

2. ComponentB.vue

<template>
  <div class="component-b">
    <h2>组件 B</h2>
    <p>这是第二个组件的内容</p>
  </div>
</template>

<style scoped>
.component-b {
  padding: 20px;
  background-color: #fce4ec;
  border: 2px solid #e91e63;
  border-radius: 8px;
  margin: 10px;
}
</style>

粉色主题的组件。

3. ComponentC.vue

<template>
  <div class="component-c">
    <h2>组件 C</h2>
    <p>这是第三个组件的内容</p>
  </div>
</template>

<style scoped>
.component-c {
  padding: 20px;
  background-color: #e8f5e9;
  border: 2px solid #4caf50;
  border-radius: 8px;
  margin: 10px;
}
</style>

绿色主题的组件。

4. ConditionalRendering.vue(主组件)

<template>
  <div class="container">
    <h1 align="center">条件渲染动态切换组件</h1>
    <div class="button-group">
      <button @click="currentComponent = 'A'" :class="{ active: currentComponent === 'A' }">显示组件 A</button>
      <button @click="currentComponent = 'B'" :class="{ active: currentComponent === 'B' }">显示组件 B</button>
      <button @click="currentComponent = 'C'" :class="{ active: currentComponent === 'C' }">显示组件 C</button>
    </div>
    <div class="component-container">
      <ComponentA v-if="currentComponent === 'A'" />
      <ComponentB v-else-if="currentComponent === 'B'" />
      <ComponentC v-else />
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import ComponentA from './ComponentA.vue'
import ComponentB from './ComponentB.vue'
import ComponentC from './ComponentC.vue'

const currentComponent = ref('A')
</script>

<style scoped>
.container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.button-group {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin: 20px 0;
}

button {
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
  border: 2px solid #ddd;
  background-color: white;
  border-radius: 4px;
  transition: all 0.3s;
}

button:hover {
  background-color: #f5f5f5;
}

button.active {
  background-color: #2196f3;
  color: white;
  border-color: #2196f3;
}

.component-container {
  min-height: 200px;
}
</style>

代码解析

代码行 说明
第 5-7 行 三个按钮,点击时设置 currentComponent 的值
第 10-12 行 条件渲染:v-if / v-else-if / v-else 切换显示不同组件
第 19-21 行 导入三个子组件
第 23 行 初始化 currentComponent 为 'A',默认显示组件 A

动态类绑定

<button :class="{ active: currentComponent === 'A' }">显示组件 A</button>

说明:当 currentComponent === 'A' 为真时,添加 active 类,当前按钮显示蓝色选中状态。

运行效果

页面显示三个按钮:

  • "显示组件 A"(默认选中,蓝色)
  • "显示组件 B"
  • "显示组件 C"

点击不同的按钮,下方会显示对应的组件:

  • 组件 A(蓝色边框)
  • 组件 B(粉色边框)
  • 组件 C(绿色边框)

v-if vs v-show

特性 v-if v-show
渲染机制 条件为假时不渲染 DOM 始终渲染 DOM,通过 display: none 控制显示
切换开销 高(创建/销毁 DOM) 低(仅切换 CSS)
初始渲染开销 低(条件为假不渲染) 高(始终渲染)
适用场景 条件不常切换,组件较大 条件频繁切换,组件较小

条件渲染 vs 动态组件

对比项 条件渲染 动态组件
语法 v-if/v-else-if/v-else <component :is="...">
适用组件数量 不限,但代码较繁琐 更适合多个组件
可读性 直观,清晰看到每个条件 更简洁
性能 每次切换都会销毁/重建 同样会销毁/重建

总结

知识点 说明
条件渲染 使用 v-ifv-else-ifv-else 实现组件切换
动态类绑定 :class="{ active: condition }" 实现按钮选中状态
响应式变量 使用 ref() 定义响应式变量控制显示
组件导入 <script setup> 中导入子组件可直接使用

评论