欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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í)筆記

    vue.js源代碼core scedule.js學(xué)習(xí)筆記

    這篇文章主要為大家詳細(xì)介紹了vue.js源代碼core scedule.js的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • vue中的router-view父子組件傳參方式

    vue中的router-view父子組件傳參方式

    這篇文章主要介紹了vue中的router-view父子組件傳參方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Vue中使用Teleport的方法示例

    Vue中使用Teleport的方法示例

    這篇文章主要為大家介紹了Vue中使用Teleport的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • vue3中onUnmounted使用詳解

    vue3中onUnmounted使用詳解

    在Vue3中,onUnmounted是一個(gè)生命周期鉤子,它會(huì)在組件實(shí)例被卸載(unmounted)和銷(xiāo)毀之前被調(diào)用,本文給大家介紹vue3中onUnmounted使用詳解,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • 原生JS?Intersection?Observer?API實(shí)現(xiàn)懶加載

    原生JS?Intersection?Observer?API實(shí)現(xiàn)懶加載

    這篇文章主要為大家介紹了原生JS?Intersection?Observer?API實(shí)現(xiàn)懶加載示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Vue3實(shí)現(xiàn)動(dòng)態(tài)面包屑的代碼示例

    Vue3實(shí)現(xiàn)動(dòng)態(tài)面包屑的代碼示例

    這篇文章主要給大家介紹一下Vue3動(dòng)態(tài)面包屑是如何實(shí)現(xiàn)的,實(shí)現(xiàn)思路又是什么,以及發(fā)給大家介紹一下面包屑的功能,文章通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • Vue.js與Flask/Django后端配合方式

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

    在現(xiàn)代Web開(kāi)發(fā)中,Vue.js與Flask或Django配合使用,實(shí)現(xiàn)前后端分離,提高開(kāi)發(fā)效率和應(yīng)用性能,本文介紹了整合Vue.js和Flask/Django的步驟,包括環(huán)境搭建、API編寫(xiě)、項(xiàng)目配置,以及生產(chǎn)部署,此架構(gòu)不僅加快了開(kāi)發(fā)進(jìn)程,還提高了項(xiàng)目的可維護(hù)性和可擴(kuò)展性
    2024-09-09
  • vue中三種不同插槽使用小結(jié)

    vue中三種不同插槽使用小結(jié)

    本文主要介紹了vue中三種不同插槽使用小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Vue3中簡(jiǎn)單實(shí)現(xiàn)動(dòng)態(tài)添加路由

    Vue3中簡(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
  • Vue 中批量下載文件并打包的示例代碼

    Vue 中批量下載文件并打包的示例代碼

    本篇文章主要介紹了Vue 中批量下載文件并打包的示例代碼,用 ajax 將文件下載, 然后用 jszip 壓縮文件, 最后用 file-saver 生成文件,有興趣的可以了解一下
    2017-11-11

最新評(píng)論