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.js源代码core scedule.js学习笔记

    vue.js源代码core scedule.js学习笔记

    这篇文章主要为大家详细介绍了vue.js源代码core scedule.js的学习笔记,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • vue中的router-view父子组件传参方式

    vue中的router-view父子组件传参方式

    这篇文章主要介绍了vue中的router-view父子组件传参方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Vue中使用Teleport的方法示例

    Vue中使用Teleport的方法示例

    这篇文章主要为大家介绍了Vue中使用Teleport的方法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • vue3中onUnmounted使用详解

    vue3中onUnmounted使用详解

    在Vue3中,onUnmounted是一个生命周期钩子,它会在组件实例被卸载(unmounted)和销毁之前被调用,本文给大家介绍vue3中onUnmounted使用详解,感兴趣的朋友跟随小编一起看看吧
    2024-03-03
  • 原生JS Intersection Observer API实现懒加载

    原生JS Intersection Observer API实现懒加载

    这篇文章主要为大家介绍了原生JS Intersection Observer API实现懒加载示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Vue3实现动态面包屑的代码示例

    Vue3实现动态面包屑的代码示例

    这篇文章主要给大家介绍一下Vue3动态面包屑是如何实现的,实现思路又是什么,以及发给大家介绍一下面包屑的功能,文章通过代码示例介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • Vue.js与Flask/Django后端配合方式

    Vue.js与Flask/Django后端配合方式

    在现代Web开发中,Vue.js与Flask或Django配合使用,实现前后端分离,提高开发效率和应用性能,本文介绍了整合Vue.js和Flask/Django的步骤,包括环境搭建、API编写、项目配置,以及生产部署,此架构不仅加快了开发进程,还提高了项目的可维护性和可扩展性
    2024-09-09
  • vue中三种不同插槽使用小结

    vue中三种不同插槽使用小结

    本文主要介绍了vue中三种不同插槽使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • Vue3中简单实现动态添加路由

    Vue3中简单实现动态添加路由

    本文主要介绍了Vue3中简单实现动态添加路由,,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • Vue 中批量下载文件并打包的示例代码

    Vue 中批量下载文件并打包的示例代码

    本篇文章主要介绍了Vue 中批量下载文件并打包的示例代码,用 ajax 将文件下载, 然后用 jszip 压缩文件, 最后用 file-saver 生成文件,有兴趣的可以了解一下
    2017-11-11

最新评论