Vue動(dòng)態(tài)組件?component?:is的使用代碼示范
vue 動(dòng)態(tài)組件用于實(shí)現(xiàn)在指定位置上,動(dòng)態(tài)加載不同的組件,核心代碼為:
<component :is="componentTag"></component>
data() {
return {
componentTag: '',
}
}componentTag 為自定義的變量,將需要加載的組件名賦值給它,即可在<component />標(biāo)簽出現(xiàn)的位置,渲染該組件。
代碼示范
<template>
<div style="padding: 30px">
<button @click="change('1')">組件1</button>
<button @click="change('2')">組件2</button>
<button @click="change('3')">組件3</button>
<component :is="componentTag"></component>
</div>
</template>
<script>
import component1 from './component1'
import component2 from './component2'
import component3 from './component3'
export default {
components: {component1, component2, component3},
data() {
return {
componentTag: '',
}
},
methods: {
change(index) {
this.componentTag = 'component' + index
},
}
}
</script>
<style scoped>
</style>src/page/component1.vue
<template>
<div>
<h3>組件1—文字</h3>
<span>我愛(ài)你,中國(guó)!</span>
</div>
</template>
<script>
export default {
name: "component1"
}
</script>
<style scoped>
</style>src/page/component2.vue
<template>
<div>
<h3>組件2-圖片</h3>
<img src="https://ss2.bdstatic.com/70cFvnSh.jpg" alt="">
</div>
</template>
<script>
export default {
name: "component2"
}
</script>
<style scoped>
</style>src/page/component3.vue
<template>
<div>
<h3>組件3—輸入框</h3>
<input type="text">
</div>
</template>
<script>
export default {
name: "component3"
}
</script>
<style scoped>
</style>效果展示
點(diǎn)擊按鈕組件1

點(diǎn)擊按鈕組件2

點(diǎn)擊按鈕組件3

以上內(nèi)容轉(zhuǎn)自:vue 動(dòng)態(tài)組件【詳解】component :is
component :is用法進(jìn)階之組件內(nèi)引入多個(gè)組件
<component :is="detailComponentName" />
import components from './components'
export default {
components: {
...components
}
}src/components/index.js
const ctx = require.context('./common', false, /\.vue$/)
const components = {}
console.log(ctx, 'ctx---打印出./common文件下(不包含子文件夾),以.vue結(jié)尾的文件')
console.log(
ctx.keys(),
'ctx.keys()---返回./common文件下(不包含子文件夾),以.vue結(jié)尾的文件的數(shù)組'
)
for (const key of ctx.keys()) {
// 剝?nèi)ノ募_(kāi)頭的 `./` 和`.vue`結(jié)尾的擴(kuò)展名
const module = key.replace(/^\.\//, '').replace(/\.vue$/, '')
components[module] = ctx(key).default
console.log(module, 'module---去掉`./`開(kāi)頭 和`.vue`結(jié)尾后的文件名')
console.log(
components[module],
'components[module]---拿到ctx文件(包括html和default)'
)
}
console.log(components, 'components---這些ctx文件集合')
export default components此處解釋該index.js文件:
require.context( directory, useSubdirectories, regExp )
- directory{String}-讀取文件的路徑。
- useSubdirectories{Boolean}-是否遍歷文件的子目錄。
- regExp{RegExp}-匹配文件的正則。
require.context('./', false, /\.vue$/) 檢索components文件下的文件,不檢索子文件夾,匹配以.vue結(jié)尾的文件。




到此這篇關(guān)于Vue動(dòng)態(tài)組件 component :is的使用的文章就介紹到這了,更多相關(guān)Vue component :is 使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3配置全局參數(shù)(掛載全局方法)以及組件的使用
這篇文章主要介紹了vue3配置全局參數(shù)(掛載全局方法)以及組件的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
vue用h()函數(shù)創(chuàng)建Vnodes的實(shí)現(xiàn)
Vue提供了一個(gè)h()函數(shù)用于創(chuàng)建vnodes,本文就來(lái)介紹一下vue用h()函數(shù)創(chuàng)建Vnodes的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
vue?element-ui動(dòng)態(tài)橫向統(tǒng)計(jì)表格的實(shí)現(xiàn)
這篇文章主要介紹了vue?element-ui動(dòng)態(tài)橫向統(tǒng)計(jì)表格的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
在vue項(xiàng)目創(chuàng)建的后初始化首次使用stylus安裝方法分享
下面小編就為大家分享一篇在vue項(xiàng)目創(chuàng)建的后初始化首次使用stylus安裝方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
vue vue-Router默認(rèn)hash模式修改為history需要做的修改詳解
今天小編就為大家分享一篇vue vue-Router默認(rèn)hash模式修改為history需要做的修改詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue使用vue-video-player無(wú)法播放本地視頻的問(wèn)題及解決
這篇文章主要介紹了vue使用vue-video-player無(wú)法播放本地視頻的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue Treeselect下拉樹(shù)只能選擇第N級(jí)元素實(shí)現(xiàn)代碼
這篇文章主要介紹了vue Treeselect下拉樹(shù)只能選擇第N級(jí)元素實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08

