前端架構(gòu)vue動態(tài)組件使用基礎(chǔ)教程
1、基本使用
新建組件 Article.vue
<template> <div> <p>黃州東南三十里為沙湖,亦曰螺師店。予買田其間,因往相田得疾。</p> <p>聞麻橋人龐安常善醫(yī)而聾。遂往求療。</p> <p>安常雖聾,而穎悟絕人,以紙畫字,書不數(shù)字,輒深了人意。</p> <p>余戲之曰:“余以手為口,君以眼為耳,皆一時異人也?!?lt;/p> <p>疾愈,與之同游清泉寺。</p> <p>寺在蘄水郭門外二里許,有王逸少洗筆泉,水極甘,下臨蘭溪,溪水西流。</p> <p>余作歌云:“山下蘭芽短浸溪,松間沙路凈無泥,蕭蕭暮雨子規(guī)啼。</p> <p>誰道人生無再少?君看流水尚能西,休將白發(fā)唱黃雞?!?lt;/p> <p>是日劇飲而歸。</p> </div> </template>
新建組件 Poetry.vue
<template> <div> <p>春宵</p> <p>蘇軾 </p> <p>春宵一刻值千金,花有清香月有陰。</p> <p>歌管樓臺聲細(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>
動態(tài)組件,即使用 component 標(biāo)簽,通過 is 屬性指定的名稱來切換不同的組件
運(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)">詩歌</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)行效果,會發(fā)現(xiàn)在 稿件 中輸入文字后,切回到 詩歌,再回到 稿件,之前的 稿件 信息就不見了
出現(xiàn)這個情況的原因是,每次切換新組件的時候,Vue 都創(chuàng)建了一個新的組件。因此,如果需要保存原來的組件信息,要配合 keep-alive 使用
添加 keep-alive 后的 Learn.vue
使用 <keep-alive>
標(biāo)簽將動態(tài)組件包裹起來
<template> <div> <keep-alive> <component :is="currentComponent"></component> </keep-alive> <button @click="changeComponent(1)">詩歌</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動態(tài)組件使用基礎(chǔ)教程的詳細(xì)內(nèi)容,更多關(guān)于前端架構(gòu)vue動態(tài)組件教程的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Vue3實(shí)現(xiàn)掃碼槍掃碼并生成二維碼實(shí)例代碼
vue3生成二維碼的方式有很多種,下面這篇文章主要給大家介紹了關(guān)于如何基于Vue3實(shí)現(xiàn)掃碼槍掃碼并生成二維碼的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06vue el-table實(shí)現(xiàn)多選框回填的示例代碼
摘要:Vue多選框回填是實(shí)現(xiàn)表單數(shù)據(jù)高效處理的常見需求,本文主要介紹了vue el-table實(shí)現(xiàn)多選框回填的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-01-01Vue項目引用百度地圖并實(shí)現(xiàn)搜索定位等功能(案例分析)
這篇文章主要介紹了Vue項目引用百度地圖并實(shí)現(xiàn)搜索定位等功能(案例分析),本篇文章為案例分析,技術(shù)點(diǎn)較多,所以篇幅較長,認(rèn)真閱覽的你一定會學(xué)到很多知識,需要的朋友可以參考下2022-09-09vue前端頁面數(shù)據(jù)加載添加loading效果的實(shí)現(xiàn)
這篇文章主要介紹了vue前端頁面數(shù)據(jù)加載添加loading效果的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07