Vue3_16(动态组件)
2022-06-15 本文已影响0人
BingJS
什么是动态组件 就是:让多个组件使用同一个挂载点,并动态切换,这就是动态组件。
在挂载点使用component标签,然后使用v-bind:is=”组件”
用法如下:
引入组件
import A from './A.vue'
import B from './B.vue'
<component :is="A"></component>
通过is 切换 A B 组件
使用场景
tab切换 居多
注意事项
- 在Vue2 的时候is 是通过组件名称切换的 在Vue3 setup 是通过组件实例切换的
- 如果你把组件实例放到Reactive Vue会给你一个警告runtime-core.esm-bundler.js:38 [Vue warn]: Vue received a Component which was made a reactive object. This can lead to unnecessary performance overhead, and should be avoided by marking the component with
markRaw
or usingshallowRef
instead ofref
.
Component that was made reactive:
这是因为reactive 会进行proxy 代理 而我们组件代理之后毫无用处 节省性能开销 推荐我们使用shallowRef 或者 markRaw 跳过proxy 代理
修改如下:
const tab = reactive<Com[]>([{
name: "A组件",
comName: markRaw(A)
}, {
name: "B组件",
comName: markRaw(B)
}])