vue實(shí)現(xiàn)組件切換效果的三種功能
更新時(shí)間:2024年11月18日 10:50:33 作者:想你的風(fēng)吹到了瑞士
這篇文章主要為大家介紹了在Vue中實(shí)現(xiàn)組件切換的三種方法,即使用條件渲染,使用動(dòng)態(tài)組件以及通過(guò)點(diǎn)擊按鈕切換組件,有需要的小伙伴可以了解下
一、使用條件渲染 (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>
二、使用動(dòng)態(tài)組件 (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>
三、點(diǎn)擊按鈕切換組件
<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>
到此這篇關(guān)于vue實(shí)現(xiàn)組件切換效果的三種功能的文章就介紹到這了,更多相關(guān)vue組件切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js源代碼core scedule.js學(xué)習(xí)筆記
這篇文章主要為大家詳細(xì)介紹了vue.js源代碼core scedule.js的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07原生JS?Intersection?Observer?API實(shí)現(xiàn)懶加載
這篇文章主要為大家介紹了原生JS?Intersection?Observer?API實(shí)現(xiàn)懶加載示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Vue3實(shí)現(xiàn)動(dòng)態(tài)面包屑的代碼示例
這篇文章主要給大家介紹一下Vue3動(dòng)態(tài)面包屑是如何實(shí)現(xiàn)的,實(shí)現(xiàn)思路又是什么,以及發(fā)給大家介紹一下面包屑的功能,文章通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07Vue3中簡(jiǎn)單實(shí)現(xiàn)動(dòng)態(tài)添加路由
本文主要介紹了Vue3中簡(jiǎn)單實(shí)現(xiàn)動(dòng)態(tài)添加路由,,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05