前端架構(gòu)vue動(dòng)態(tài)組件使用基礎(chǔ)教程
1、基本使用
新建組件 Article.vue
<template>
<div>
<p>黃州東南三十里為沙湖,亦曰螺師店。予買(mǎi)田其間,因往相田得疾。</p>
<p>聞麻橋人龐安常善醫(yī)而聾。遂往求療。</p>
<p>安常雖聾,而穎悟絕人,以紙畫(huà)字,書(shū)不數(shù)字,輒深了人意。</p>
<p>余戲之曰:“余以手為口,君以眼為耳,皆一時(shí)異人也?!?lt;/p>
<p>疾愈,與之同游清泉寺。</p>
<p>寺在蘄水郭門(mén)外二里許,有王逸少洗筆泉,水極甘,下臨蘭溪,溪水西流。</p>
<p>余作歌云:“山下蘭芽短浸溪,松間沙路凈無(wú)泥,蕭蕭暮雨子規(guī)啼。</p>
<p>誰(shuí)道人生無(wú)再少?君看流水尚能西,休將白發(fā)唱黃雞。”</p>
<p>是日劇飲而歸。</p>
</div>
</template>新建組件 Poetry.vue
<template>
<div>
<p>春宵</p>
<p>蘇軾 </p>
<p>春宵一刻值千金,花有清香月有陰。</p>
<p>歌管樓臺(tái)聲細(xì)細(xì),秋千院落夜沉沉。</p>
</div>
</template>新建 Learn.vue
并在 Learn.vue 中引入 Article.vue 和 Poetry.vue
本文中 Learn.vue 、Article.vue、Poetry.vue 在同一文件夾下
<template>
<div>
<component :is="currentComponent"></component>
<button @click="changeComponent">修改組件</button>
</div>
</template>
<script>
import Article from './Article.vue'
import Poetry from './Poetry.vue'
export default {
components: {
Article,
Poetry
},
data() {
return {
currentComponent: 'Article'
}
},
methods: {
changeComponent() {
this.currentComponent = 'Poetry'
}
}
}
</script>動(dòng)態(tài)組件,即使用 component 標(biāo)簽,通過(guò) is 屬性指定的名稱來(lái)切換不同的組件
運(yùn)行效果

2、配合 keep-alive 使用
keep-alive 可以保持這些組件的狀態(tài),如果需要保持組件的狀態(tài),則需要配合 keep-alive 一起使用
先看需要保持狀態(tài),而不使用 keep-alive 的情況
新建 Manuscript.vue
<template>
<div>
<form action="">
<input type="text" name="title" />
<br>
<input type="text" name="content" />
</form>
</div>
</template>修改 Learn.vue
<template>
<div>
<component :is="currentComponent"></component>
<button @click="changeComponent(1)">詩(shī)歌</button>
<button @click="changeComponent(2)">稿件</button>
</div>
</template>
<script>
import Poetry from './Poetry.vue'
import Manuscript from './Manuscript.vue'
export default {
components: {
Poetry,
Manuscript
},
data() {
return {
currentComponent: 'Poetry'
}
},
methods: {
changeComponent(type) {
if(type == 1) {
this.currentComponent = 'Poetry'
}
if(type == 2) {
this.currentComponent = 'Manuscript'
}
}
}
}
</script>運(yùn)行效果

看運(yùn)行效果,會(huì)發(fā)現(xiàn)在 稿件 中輸入文字后,切回到 詩(shī)歌,再回到 稿件,之前的 稿件 信息就不見(jiàn)了
出現(xiàn)這個(gè)情況的原因是,每次切換新組件的時(shí)候,Vue 都創(chuàng)建了一個(gè)新的組件。因此,如果需要保存原來(lái)的組件信息,要配合 keep-alive 使用
添加 keep-alive 后的 Learn.vue
使用 <keep-alive> 標(biāo)簽將動(dòng)態(tài)組件包裹起來(lái)
<template>
<div>
<keep-alive>
<component :is="currentComponent"></component>
</keep-alive>
<button @click="changeComponent(1)">詩(shī)歌</button>
<button @click="changeComponent(2)">稿件</button>
</div>
</template>
<script>
import Poetry from './Poetry.vue'
import Manuscript from './Manuscript.vue'
export default {
components: {
Poetry,
Manuscript
},
data() {
return {
currentComponent: 'Poetry'
}
},
methods: {
changeComponent(type) {
if(type == 1) {
this.currentComponent = 'Poetry'
}
if(type == 2) {
this.currentComponent = 'Manuscript'
}
}
}
}
</script>運(yùn)行效果

以上就是前端架構(gòu)vue動(dòng)態(tài)組件使用基礎(chǔ)教程的詳細(xì)內(nèi)容,更多關(guān)于前端架構(gòu)vue動(dòng)態(tài)組件教程的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Vue動(dòng)態(tài)組件component標(biāo)簽的用法大全
- Vue動(dòng)態(tài)組件和keep-alive組件實(shí)例詳解
- Vue動(dòng)態(tài)組件component的深度使用說(shuō)明
- Vue高級(jí)用法實(shí)例教程之動(dòng)態(tài)組件
- vue?內(nèi)置組件?component?的用法示例詳解
- 手寫(xiě)Vue內(nèi)置組件component的實(shí)現(xiàn)示例
- 詳解Vue新增內(nèi)置組件的使用
- Vue 內(nèi)置組件keep-alive的使用示例
- Vue動(dòng)態(tài)組件與內(nèi)置組件淺析講解
相關(guān)文章
vue手機(jī)端input change時(shí),無(wú)法執(zhí)行的問(wèn)題及解決
這篇文章主要介紹了vue手機(jī)端input change時(shí),無(wú)法執(zhí)行的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Vue-router不允許導(dǎo)航到當(dāng)前位置(/path)錯(cuò)誤原因以及修復(fù)方式
本文主要介紹了Vue-router不允許導(dǎo)航到當(dāng)前位置(/path)錯(cuò)誤原因以及修復(fù)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
VUE-PDF實(shí)現(xiàn)pdf在線預(yù)覽問(wèn)題
這篇文章主要介紹了VUE-PDF實(shí)現(xiàn)pdf在線預(yù)覽問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue-cli啟動(dòng)本地服務(wù)局域網(wǎng)不能訪問(wèn)的原因分析
這篇文章主要介紹了vue-cli啟動(dòng)本地服務(wù),局域網(wǎng)下訪問(wèn)不到的原因分析,在文中還給大家介紹了vue-cli起的webpack項(xiàng)目 用localhost可以訪問(wèn),但是切換到ip就不可以訪問(wèn) 的原因,本文給大家介紹的非常詳細(xì),需要的朋友參考下2018-01-01
使用 webpack 插件自動(dòng)生成 vue 路由文件的方法
這篇文章主要介紹了使用 webpack 插件自動(dòng)生成 vue 路由文件的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
vue-cli3+typescript初體驗(yàn)小結(jié)
這篇文章主要介紹了vue-cli3+typescript初體驗(yàn)小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Vue中使用JsonView來(lái)展示Json樹(shù)的實(shí)例代碼
這篇文章主要介紹了Vue之使用JsonView來(lái)展示Json樹(shù)的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
解決Vue3?echarts?v-show無(wú)法重新渲染的問(wèn)題
這篇文章主要介紹了Vue3?echarts?v-show無(wú)法重新渲染的問(wèn)題,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09

