vue動(dòng)態(tài)組件的實(shí)現(xiàn)
標(biāo)簽:
JavaScript
在vue中组件是可以通过component标签来进行动态切换的。
components文件夹下创建component和list组件,然后在main.js中设置成为公共组件(具体公共组件相关的在之前的手记有)
这样在各个vue文件中都可以直接使用该组件了。
import ListItem from './components/list.vue'//封装共用组件方法一(共用组件) Vue.component('ListItem',ListItem) import child from './components/component/component.js'//封装共用组件方法二(插件) Vue.use(child)
这里我将组建的名称定义成了‘ListItem’和‘child’这两个名字。
动态组件的实现
data中定义一个组件名称的字符串componentStr然后将该字段绑定到component这个标签的is字段上,通过改变componentStr的名字来切换对应的组件。
<button @click="componentStr == 'ListItem' ? componentStr = 'child' : componentStr = 'ListItem'">切换组件</button> <!-- 动态组件 --> <component :is="componentStr"></component>
动态组件的使用场景有很多,掌握了动态组件对往后的项目会有很大的帮助。
點(diǎn)擊查看更多內(nèi)容
為 TA 點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦