当前位置:首页 > 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 的路由配置实现页面级组件切换:

vue实现切换组件

  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 });

vue实现切换组件

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>

动画过渡效果

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

<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实现tab标签

vue实现tab标签

Vue 实现 Tab 标签的方法 使用动态组件和 v-for 渲染标签 通过 v-for 循环生成标签页标题,结合 v-bind:class 动态切换激活状态。使用 v-show 或动态组件 <…

vue实现多级表头

vue实现多级表头

Vue 实现多级表头的方法 使用 el-table 组件 Element UI 的 el-table 组件支持多级表头的实现,通过嵌套 el-table-column 即可完成。 <el-t…

vue实现按钮刷新

vue实现按钮刷新

Vue 实现按钮刷新功能 在 Vue 中实现按钮刷新功能可以通过多种方式实现,具体取决于刷新需求是局部刷新还是全局刷新。以下是几种常见的方法: 使用 window.location.reload(…

vue实现访问量

vue实现访问量

Vue 实现访问量统计的方法 使用前端计数 在 Vue 中可以通过简单的计数器实现页面访问量的统计。这种方法适合小型项目或演示用途。 // 在 Vue 组件中 data() { return {…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm ins…

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…