Vue動態(tài)組件component標(biāo)簽的用法大全
簡介
說明
本文介紹Vue的動態(tài)組件的用法。
在Vue中,可以通過component標(biāo)簽的is屬性動態(tài)指定標(biāo)簽,例如:
<component :is="componentName"></component>
此時,componentName的值是什么,就會引入什么組件。
官網(wǎng)網(wǎng)址
https://v2.cn.vuejs.org/v2/guide/components.html#動態(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)來的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)來的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>
測試
到此這篇關(guān)于Vue動態(tài)組件component標(biāo)簽的用法大全的文章就介紹到這了,更多相關(guān)Vue--動態(tài)組件component標(biāo)簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
手寫Vue源碼之?dāng)?shù)據(jù)劫持示例詳解
這篇文章主要給大家介紹了手寫Vue源碼之?dāng)?shù)據(jù)劫持的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01vue前端項目打包成Docker鏡像并運行的實現(xiàn)
這篇文章主要介紹了vue前端項目打包成Docker鏡像并運行的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08vue路由傳參方式的方式總結(jié)及獲取參數(shù)詳解
vue 路由傳參的使用場景一般都是應(yīng)用在父路由跳轉(zhuǎn)到子路由時,攜帶參數(shù)跳轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于vue路由傳參方式的方式總結(jié)及獲取參數(shù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07vue-cli-service serve報錯error:0308010C:digital enve
這篇文章主要介紹了vue-cli-service serve報錯error:0308010C:digital envelope routines::unsupported的解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06vue項目中swiper輪播active圖片實現(xiàn)居中并放大
這篇文章主要介紹了vue項目中swiper輪播active圖片實現(xiàn)居中并放大方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05