vue实现组件切换效果的三种功能

 更新时间:2024年11月18日 10:50:33   作者:想你的风吹到了瑞士  
这篇文章主要为大家介绍了在Vue中实现组件切换的三种方法,即使用条件渲染,使用动态组件以及通过点击按钮切换组件,有需要的小伙伴可以了解下

一、使用条件渲染 (v-if)

<template>
    <div>
        <button @click="currentView = 'ComponentA'">Show Component A</button>
        <button @click="currentView = 'ComponentB'">Show Component B</button>
        <component-a v-if="currentView === 'ComponentA'"></component-a>
        <component-b v-if="currentView === 'ComponentB'"></component-b>
    </div>
</template>
<script>
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';
export default {
    data() {
        return {
            currentView: 'ComponentA'
        };
    },
    components: {
        ComponentA,
        ComponentB
    }
};
</script>

二、使用动态组件 (component)

<template>
    <div>
        <button @click="currentView = 'ComponentA'">Show Component A</button>
 
        <button @click="currentView = 'ComponentB'">Show Component B</button>
 
        <component :is="currentView"></component>
    </div>
</template>
 
<script>
import ComponentA from './ComponentA.vue';
 
import ComponentB from './ComponentB.vue';
 
export default {
    data() {
        return {
            currentView: 'ComponentA'
        };
    },
 
    components: {
        ComponentA,
 
        ComponentB
    }
};
</script>

 三、点击按钮切换组件

<template>
  <div>
    <button @click="toggleComponent">切换组件</button>
    <div v-if="showComponent">
      <ComponentA />
    </div>
    <div v-else>
      <ComponentB />
    </div>
  </div>
</template>
 
<script>
import ComponentA from './ComponentA.vue'
import ComponentB from './ComponentB.vue'
 
export default {
  data() {
    return {
      showComponent: true
    }
  },
  methods: {
    toggleComponent() {
      this.showComponent = !this.showComponent
    }
  },
  components: {
    ComponentA,
    ComponentB
  }
}
</script>
<template>
  <div>
    <button @click="toggleComponent">切换组件</button>
    <transition name="fade">
      <component :is="currentComponent" />
    </transition>
  </div>
</template>
 
<script>
import ComponentA from './ComponentA.vue'
import ComponentB from './ComponentB.vue'
 
export default {
  data() {
    return {
      currentComponent: 'ComponentA'
    }
  },
  methods: {
    toggleComponent() {
      this.currentComponent = this.currentComponent === 'ComponentA' ? 'ComponentB' : 'ComponentA'
    }
  },
  components: {
    ComponentA,
    ComponentB
  }
}
</script>
 
<style>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}
 
.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>

到此这篇关于vue实现组件切换效果的三种功能的文章就介绍到这了,更多相关vue组件切换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于Vue+Element Plus实现组件递归调用的详细步骤

    基于Vue+Element Plus实现组件递归调用的详细步骤

    在前端开发中,递归是一种非常强大的编程技术,它允许函数或组件调用自身来解决问题,在Vue.js生态中,结合Element Plus UI库,我们可以利用组件递归调用来构建复杂的树形结构,本文将深入探讨Vue3与 Element Plus中组件递归调用的实现原理、详细步骤、最佳实践
    2025-07-07
  • Element input树型下拉框的实现代码

    Element input树型下拉框的实现代码

    这篇文章主要介绍了Element input树型下拉框的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • 浅谈Vant-list 上拉加载及下拉刷新的问题

    浅谈Vant-list 上拉加载及下拉刷新的问题

    这篇文章主要介绍了浅谈Vant-list 上拉加载及下拉刷新的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • Vite处理html模板插件之vite-plugin-html插件使用

    Vite处理html模板插件之vite-plugin-html插件使用

    这篇文章主要给大家介绍了关于Vite处理html模板插件之vite-plugin-html插件使用的相关资料,Vite是一个现代化的前端构建工具,而vite-plugin-html是Vite的一个插件,用于在构建时自动生成HTML文件,需要的朋友可以参考下
    2023-10-10
  • unplugin-auto-import的配置以及eslint报错解决详解

    unplugin-auto-import的配置以及eslint报错解决详解

    unplugin-auto-import 解决了vue3-hook、vue-router、useVue等多个插件的自动导入,也支持自定义插件的自动导入,是一个功能强大的typescript支持工具,这篇文章主要给大家介绍了关于unplugin-auto-import的配置以及eslint报错解决的相关资料,需要的朋友可以参考下
    2022-08-08
  • vue多页面项目开发实战指南

    vue多页面项目开发实战指南

    一般我们的vue项目都是单页面的,其实因为vue在工程化开发的时候依赖了webpack,webpack帮我们将所有的资源整合到一起而形成一个单页面,下面这篇文章主要给大家介绍了关于vue多页面项目开发的相关资料,需要的朋友可以参考下
    2022-01-01
  • vue+vuex+axios实现登录、注册页权限拦截

    vue+vuex+axios实现登录、注册页权限拦截

    下面小编就为大家分享一篇vue+vuex+axios实现登录、注册页权限拦截,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • vue项目中路由跳转页面不变问题及解决

    vue项目中路由跳转页面不变问题及解决

    这篇文章主要介绍了vue项目中路由跳转页面不变问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • Vue项目添加动态浏览器头部title的方法

    Vue项目添加动态浏览器头部title的方法

    这篇文章主要介绍了Vue项目添加动态浏览器头部title的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Vue组件模板的几种书写形式(3种)

    Vue组件模板的几种书写形式(3种)

    这篇文章主要介绍了Vue组件模板的几种书写形式(3种),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02

最新评论