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>
相关推荐
约定Da于配置1 小时前
uniapp封装websocket
前端·javascript·vue.js·websocket·网络协议·学习·uni-app
山楂树の1 小时前
xr-frame 模型摆放与手势控制,支持缩放旋转
前端·xr·图形渲染
大叔_爱编程1 小时前
wx030基于springboot+vue+uniapp的养老院系统小程序
vue.js·spring boot·小程序·uni-app·毕业设计·源码·课程设计
LBJ辉2 小时前
1. 小众但非常实用的 CSS 属性
前端·css
milk_yan2 小时前
Docker集成onlyoffice实现预览功能
前端·笔记·docker
计算机学姐3 小时前
基于微信小程序的驾校预约小程序
java·vue.js·spring boot·后端·spring·微信小程序·小程序
村口蹲点的阿三4 小时前
Spark SQL 中对 Map 类型的操作函数
javascript·数据库·hive·sql·spark
m0_748255024 小时前
头歌答案--爬虫实战
java·前端·爬虫
noravinsc5 小时前
python md5加密
前端·javascript·python
ac-er88885 小时前
Yii框架优化Web应用程序性能
开发语言·前端·php