Vue動態(tài)組件?component?:is的使用代碼示范
vue 動態(tài)組件用于實(shí)現(xiàn)在指定位置上,動態(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>我愛你,中國!</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 動態(tài)組件【詳解】component :is
component :is用法進(jìn)階之組件內(nèi)引入多個組件
<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)ノ募_頭的 `./` 和`.vue`結(jié)尾的擴(kuò)展名
const module = key.replace(/^\.\//, '').replace(/\.vue$/, '')
components[module] = ctx(key).default
console.log(module, 'module---去掉`./`開頭 和`.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動態(tài)組件 component :is的使用的文章就介紹到這了,更多相關(guān)Vue component :is 使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue進(jìn)度條組件實(shí)現(xiàn)代碼(可拖拽可點(diǎn)擊)
在日常開發(fā)中隨著需求的個性化,邏輯的復(fù)雜化,自定義組件也變得越來越常見,這篇文章主要給大家介紹了關(guān)于vue進(jìn)度條組件實(shí)現(xiàn)(可拖拽可點(diǎn)擊)的相關(guān)資料,需要的朋友可以參考下2023-12-12
vue forEach循環(huán)數(shù)組拿到自己想要的數(shù)據(jù)方法
今天小編就為大家分享一篇vue forEach循環(huán)數(shù)組拿到自己想要的數(shù)據(jù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
基于vue-cli3+typescript的tsx開發(fā)模板搭建過程分享
這篇文章主要介紹了搭建基于vue-cli3+typescript的tsx開發(fā)模板,本文通過實(shí)例代碼截圖的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
解決vue項(xiàng)目打包上服務(wù)器顯示404錯誤,本地沒出錯的問題
這篇文章主要介紹了解決vue項(xiàng)目打包上服務(wù)器顯示404錯誤,本地沒出錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue.js element-ui validate中代碼不執(zhí)行問題解決方法
這篇文章主要介紹了vue.js element-ui validate中代碼不執(zhí)行問題解決方法,需要的朋友可以參考下2017-12-12
Vue-router如何實(shí)現(xiàn)路由懶加載
在現(xiàn)代前端開發(fā)中,Vue.js憑借其輕量級和易用性,成為了很多開發(fā)者的首選框架,本文將結(jié)合實(shí)際案例,詳細(xì)講解Vue-Router路由懶加載的用法,需要的可以參考下2024-11-11

