今天在做tab组件的时候,偶然发现component动态组件的用法,由于平时基本没用,都忘了还有它的存在了,一翻文档,发现还特别好用,看来以后还是要多用用了。
话不多说,直接上代码:1
2
3
4
5
6
7<button @click="componentName='first'">组件2</button>
<button @click="componentName='second">组件2</button>
//这里是重点
<component :is="componentName"></component>
import first from "@/components/first"
import second from "@/components/second"
当动态改变componentName的值时,就可以渲染不同的组件内容了。