当前位置:首页 > VUE

vue实现切换组件

2026-01-20 07:28:27VUE

Vue 实现组件切换的方法

动态组件 <component :is>

使用 Vue 内置的 <component> 标签配合 :is 动态绑定组件名,通过改变 is 的值切换组件:

<template>
  <component :is="currentComponent"></component>
  <button @click="currentComponent = 'ComponentA'">切换A</button>
  <button @click="currentComponent = 'ComponentB'">切换B</button>
</template>

<script>
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';

export default {
  data() {
    return {
      currentComponent: 'ComponentA'
    };
  },
  components: { ComponentA, ComponentB }
};
</script>

条件渲染 v-if/v-show

通过 v-ifv-show 控制组件的显示与隐藏:

<template>
  <ComponentA v-if="showComponentA" />
  <ComponentB v-if="!showComponentA" />
  <button @click="showComponentA = !showComponentA">切换组件</button>
</template>

<script>
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';

export default {
  data() {
    return {
      showComponentA: true
    };
  },
  components: { ComponentA, ComponentB }
};
</script>

路由切换(Vue Router)

通过 Vue Router 的路由配置实现页面级组件切换:

  1. 安装并配置路由:
    
    // router/index.js
    import { createRouter, createWebHistory } from 'vue-router';
    import ComponentA from '../views/ComponentA.vue';
    import ComponentB from '../views/ComponentB.vue';

const routes = [ { path: '/a', component: ComponentA }, { path: '/b', component: ComponentB } ];

const router = createRouter({ history: createWebHistory(), routes });

export default router;


2. 在组件中使用 `<router-link>` 或编程式导航:
```html
<template>
  <router-link to="/a">显示A</router-link>
  <router-link to="/b">显示B</router-link>
  <router-view></router-view>
</template>

状态管理(Vuex/Pinia)

通过状态管理工具全局管理当前显示的组件:

// store.js (Pinia示例)
import { defineStore } from 'pinia';

export const useComponentStore = defineStore('component', {
  state: () => ({
    current: 'ComponentA'
  }),
  actions: {
    toggle() {
      this.current = this.current === 'ComponentA' ? 'ComponentB' : 'ComponentA';
    }
  }
});
<template>
  <ComponentA v-if="store.current === 'ComponentA'" />
  <ComponentB v-if="store.current === 'ComponentB'" />
  <button @click="store.toggle()">切换</button>
</template>

<script setup>
import { useComponentStore } from './store';
const store = useComponentStore();
</script>

动画过渡效果

为组件切换添加过渡动画:

vue实现切换组件

<template>
  <transition name="fade" mode="out-in">
    <component :is="currentComponent"></component>
  </transition>
</template>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
</style>

标签: 组件vue
分享给朋友:

相关文章

vue组件传值实现分页

vue组件传值实现分页

Vue 组件传值实现分页 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方法: 使用props和$emit 父组件通过props向子组件传递数据,子组件通过$emit向…

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="cloc…

vue实现贴图

vue实现贴图

Vue 实现贴图功能 在Vue中实现贴图功能通常涉及将图片动态添加到指定位置或元素上。以下是几种常见实现方式: 使用v-bind动态绑定图片 通过数据驱动的方式动态更换图片源: <templ…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…