Vue3 动态组件

动态组件指的是:同一个页面使用多个组件,且可以动态切换。
使用场景:动态组件多用于 tabs标签页

1、在页面中使用 component 标签,然后使用 :is=“组件名”

import A from './components/A.vue'
import B from './components/B.vue'
import C from './components/C.vue'
<component :is="curCom"></component>

const curCom = ref(shallowRef(A))

由于 component 在Vue3 setup 是通过组件实例切换的,如果直接用 reactive 或者 ref 直接包裹组件的话,会使得组件信息也会被劫持,但是其实这些劫持是没有必要的,会造成性能上的浪费,此时Vue会给出警告,正确的方式是:使用 shallowRef 和 markRaw 搭配。

完整代码
A.vue

<template>
  <div class="a-box">我是A组件</div>
</template>

<script setup lang="ts"></script>

<style scoped lang="scss">
.a-box {
  width: 400px;
  height: 200px;
  border: 1px solid #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

B.vue

<template>
  <div class="b-box">我是B组件</div>
</template>

<script setup lang="ts"></script>

<style scoped lang="scss">
.b-box {
  width: 400px;
  height: 200px;
  border: 1px solid #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

C.vue

<template>
  <div class="c-box">我是C组件</div>
</template>

<script setup lang="ts"></script>

<style scoped lang="scss">
.c-box {
  width: 400px;
  height: 200px;
  border: 1px solid #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

App.vue

<template>
  <div class="tabs">
    <div
      @click="switchCom(item, index)"
      :class="[active == index ? 'active' : '']"
      class="tabs-item"
      v-for="(item, index) in data"
    >
      <div>{{ item.name }}</div>
    </div>
  </div>
  <component :is="curCom"></component>
</template>

<script setup lang="ts">
import { markRaw, reactive, ref, shallowRef } from 'vue'
import A from './components/A.vue'
import B from './components/B.vue'
import C from './components/C.vue'

const curCom = ref(shallowRef(A))
const active = ref(0)

const data = reactive([
  {
    name: 'A组件',
    com: markRaw(A),
  },
  {
    name: 'B组件',
    com: markRaw(B),
  },
  {
    name: 'C组件',
    com: markRaw(C),
  },
])

const switchCom = (item: any, index: number) => {
  curCom.value = item.com
  active.value = index
}
</script>

<style lang="scss">
.tabs {
  display: flex;
  .tabs-item {
    padding: 5px;
    border: 1px solid #ccc;
    margin: 5px;
  }
  .active {
    background: #6690ec;
  }
}
</style>

2、使用 v-if 或 v-show 来动态渲染组件

import A from './components/A.vue'
import B from './components/B.vue'
import C from './components/C.vue'
<A v-if="curCom == 'A'"></A>
<B v-if="curCom == 'B'"></B>
<C v-if="curCom == 'C'"></C>

const curCom = ref('A')

完整代码

<template>
  <div class="tabs">
    <div
      @click="switchCom(item)"
      :class="[curCom == item.com ? 'active' : '']"
      class="tabs-item"
      v-for="item in data"
    >
      <div>{{ item.name }}</div>
    </div>
  </div>
  <A v-if="curCom == 'A'"></A>
  <B v-if="curCom == 'B'"></B>
  <C v-if="curCom == 'C'"></C>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import A from './components/A.vue'
import B from './components/B.vue'
import C from './components/C.vue'

const curCom = ref('A')

const data = reactive([
  {
    name: 'A组件',
    com: 'A',
  },
  {
    name: 'B组件',
    com: 'B',
  },
  {
    name: 'C组件',
    com: 'C',
  },
])

const switchCom = (item: any) => {
  curCom.value = item.com
}
</script>

<style lang="scss">
.tabs {
  display: flex;
  .tabs-item {
    padding: 5px;
    border: 1px solid #ccc;
    margin: 5px;
  }
  .active {
    background: #6690ec;
  }
}
</style>

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!学习Vue 3动态组件是一个很不错的选择。Vue 3中的动态组件可以帮助你在运行时动态地切换不同的组件,并且非常方便易用。下面是一些学习Vue 3动态组件的基本步骤: 1. 安装Vue 3:首先,你需要在你的项目中安装Vue 3。你可以通过CDN引入Vue 3,也可以使用npm或yarn安装。 2. 创建动态组件:在Vue 3中,你可以使用`<component>`元素来创建动态组件。你可以将`is`属性设置为一个变量,该变量的值可以是一个组件的名称或组件实例。 3. 切换组件:通过改变`is`属性的值,你可以在运行时动态地切换组件。这可以通过计算属性、方法等来实现。 4. 注册组件:在使用动态组件之前,你需要确保你的组件已经注册。你可以使用`app.component`方法来全局注册组件,或者在局部组件中使用`components`选项来注册组件。 5. 渲染动态组件:最后,在模板中使用`<component>`元素并将`is`属性绑定到变量,以渲染动态组件。 这是一个简单的示例代码,演示了如何使用Vue 3动态组件: ```javascript <template> <div> <component :is="currentComponent"></component> <button @click="toggleComponent">切换组件</button> </div> </template> <script> import ComponentA from './ComponentA.vue'; import ComponentB from './ComponentB.vue'; export default { data() { return { currentComponent: 'ComponentA' }; }, components: { ComponentA, ComponentB }, methods: { toggleComponent() { this.currentComponent = this.currentComponent === 'ComponentA' ? 'ComponentB' : 'ComponentA'; } } }; </script> ``` 在这个示例中,我们创建了两个组件`ComponentA`和`ComponentB`,然后在父组件中使用`<component>`元素来动态渲染当前的组件。通过点击按钮,可以切换当前组件的显示。 希望这个示例能够帮助你开始学习Vue 3动态组件!如果你有任何问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值