Vue動態(tài)組件component標簽的用法大全
簡介
說明
本文介紹Vue的動態(tài)組件的用法。
在Vue中,可以通過component標簽的is屬性動態(tài)指定標簽,例如:
<component :is="componentName"></component>
此時,componentName的值是什么,就會引入什么組件。
官網網址
https://v2.cn.vuejs.org/v2/guide/components.html#動態(tài)組件
示例
路由設置
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>傳入進來的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>傳入進來的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>測試

到此這篇關于Vue動態(tài)組件component標簽的用法大全的文章就介紹到這了,更多相關Vue--動態(tài)組件component標簽內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue前端項目打包成Docker鏡像并運行的實現(xiàn)
這篇文章主要介紹了vue前端項目打包成Docker鏡像并運行的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
vue-cli-service serve報錯error:0308010C:digital enve
這篇文章主要介紹了vue-cli-service serve報錯error:0308010C:digital envelope routines::unsupported的解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
vue項目中swiper輪播active圖片實現(xiàn)居中并放大
這篇文章主要介紹了vue項目中swiper輪播active圖片實現(xiàn)居中并放大方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05

