Vue動(dòng)態(tài)組件component標(biāo)簽的用法大全
簡(jiǎn)介
說(shuō)明
本文介紹Vue的動(dòng)態(tài)組件的用法。
在Vue中,可以通過(guò)component標(biāo)簽的is屬性動(dòng)態(tài)指定標(biāo)簽,例如:
<component :is="componentName"></component>
此時(shí),componentName的值是什么,就會(huì)引入什么組件。
官網(wǎng)網(wǎng)址
https://v2.cn.vuejs.org/v2/guide/components.html#動(dòng)態(tài)組件
示例
路由設(shè)置
router/index.js
import Vue from 'vue' import VueRouter from 'vue-router' import Parent from '../components/Parent' Vue.use(VueRouter) const routes = [ { path: '/', name: 'Parent', component: Parent } ] const router = new VueRouter({ routes }) export default router
父組件
components/Parent.vue
<template> <div class="outer"> <h2>這是父組件</h2> <component :is="componentName" :propA="propAValue"></component> </div> </template> <script> import ChildA from './ChildA' import ChildB from './ChildB' export default { name: 'Parent', components: { ChildA, ChildB }, data () { return { componentName: 'ChildB', propAValue: 'aaa' } } } </script> <style scoped> .outer { margin: 20px; border: 2px solid red; padding: 20px; } </style>
子組件
components/ChildA.vue
<template> <div class="outer"> <h3>這是ChildA</h3> <div>傳入進(jìn)來(lái)的propA值為:{{propA}}</div> </div> </template> <script> export default { name: 'ChildA', props: ['propA'] } </script> <style scoped> .outer { margin: 20px; border: 2px solid blue; padding: 20px; } </style>
components/ChildA.vue
<template> <div class="outer"> <h3>這是ChildB</h3> <div>傳入進(jìn)來(lái)的propA值為:{{propA}}</div> </div> </template> <script> export default { name: 'ChildB', props: ['propA'] } </script> <style scoped> .outer { margin: 20px; border: 2px solid blue; padding: 20px; } </style>
測(cè)試
訪問(wèn):http://localhost:8080/
到此這篇關(guān)于Vue動(dòng)態(tài)組件component標(biāo)簽的用法大全的文章就介紹到這了,更多相關(guān)Vue--動(dòng)態(tài)組件component標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
手寫(xiě)Vue源碼之?dāng)?shù)據(jù)劫持示例詳解
這篇文章主要給大家介紹了手寫(xiě)Vue源碼之?dāng)?shù)據(jù)劫持的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01vue前端項(xiàng)目打包成Docker鏡像并運(yùn)行的實(shí)現(xiàn)
這篇文章主要介紹了vue前端項(xiàng)目打包成Docker鏡像并運(yùn)行的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08一篇文章告訴你如何實(shí)現(xiàn)Vue前端分頁(yè)和后端分頁(yè)
這篇文章主要為大家介紹了如何實(shí)現(xiàn)Vue前端分頁(yè)和后端分頁(yè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12vue路由傳參方式的方式總結(jié)及獲取參數(shù)詳解
vue 路由傳參的使用場(chǎng)景一般都是應(yīng)用在父路由跳轉(zhuǎn)到子路由時(shí),攜帶參數(shù)跳轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于vue路由傳參方式的方式總結(jié)及獲取參數(shù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07vue-cli-service serve報(bào)錯(cuò)error:0308010C:digital enve
這篇文章主要介紹了vue-cli-service serve報(bào)錯(cuò)error:0308010C:digital envelope routines::unsupported的解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Vue2中無(wú)法檢測(cè)到數(shù)組變動(dòng)的原因及解決
由于某些限制,vue2不能檢測(cè)到某些情況下數(shù)組的變動(dòng),本文就將具體講解這兩種限制的解決思路2021-06-06vue項(xiàng)目中swiper輪播active圖片實(shí)現(xiàn)居中并放大
這篇文章主要介紹了vue項(xiàng)目中swiper輪播active圖片實(shí)現(xiàn)居中并放大方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05記錄vue項(xiàng)目中遇到的一點(diǎn)小問(wèn)題
本文是腳本之家小編給大家收藏整理的關(guān)于vue項(xiàng)目中遇到的一點(diǎn)小問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05