vue3中keep-alive和vue-router的結(jié)合使用方式
前言
keep-alive:Vue內(nèi)置的一個(gè)組件,可以使被包含的組件保留狀態(tài),或避免重新渲染。router-view:vue-router內(nèi)置組件, 如果直接包含在keep-alive里面,所有路徑匹配到的組件都會被緩存。
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
代碼
Hmoe組件:
<template>
<div class="home">
<input type="text" name="" id="">
</div>
</template>
<script>
export default {
name: 'Home',
components: {},
created() {
console.log('Home被創(chuàng)建');
},
unmounted() {
console.log('Home被銷毀');
}
}
</script>
About組件:
<template>
<div class="about">
<input type="text">
</div>
</template>
<script>
export default {
name: 'About',
components: {},
created() {
console.log('About被創(chuàng)建');
},
unmounted() {
console.log('About被銷毀');
}
}
</script>
路由代碼:
<template>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view/>
</template>
一、為何要使用keep-alive?
當(dāng)路由切換時(shí)想要達(dá)到頁面不重新緩存,避免組件被銷毀時(shí)即可使用keep-alive來實(shí)現(xiàn)。
例如:

此時(shí)切換到“About”組件后“Home”逐漸將會被銷毀,

再切換回“Home”組件時(shí)輸入框內(nèi)信息已經(jīng)被重新渲染消去。
為避免組件重新渲染所以使用“keep-alive”。
二、vue2中使用keep-alive
將“router-view”組件包含于“keep-alive”即可
<keep-alive>
<router-view />
</keep-alive>
此時(shí)組件將保留狀態(tài),或避免重新渲染。
三、vue3中使用keep-alive
vue3的keep-alive應(yīng)用相對于vue2有所變化,此處描述vue3時(shí)如何使用,詳情可見:Vue Router文檔
使
<keep-alive>
<router-view />
</keep-alive>
改變?yōu)槿缦麓a,
<router-view v-slot="{ Component }">
<transition>
<keep-alive>
<component :is="Component" />
</keep-alive>
</transition>
</router-view>
即可。
四、keep-alive屬性“include,exclude”的使用
注意:使用include,exclude 屬性需要給所有vue類的name賦值,否則 include,exclude將不生效
include值為字符串或者正則表達(dá)式匹配的組件name不會被銷毀。exclude值為字符串或正則表達(dá)式匹配的組件name會被銷毀。
如,修改路由代碼為:
<template>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view v-slot="{ Component }">
<transition>
//About組件將會被銷毀,而Home組件則不會
<keep-alive exclude="About">
<component :is="Component" />
</keep-alive>
</transition>
</router-view>
</template>
例如:
進(jìn)入頁面

切換路由至“About”

“Home”組件未被銷毀,再切換路由至“Home”

“About”組件被銷毀
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Element封裝一個(gè)表格組件tableList的使用方法
這篇文章主要介紹了基于Element封裝一個(gè)表格組件tableList的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
vue父組件給子組件的組件傳值provide inject的方法
在本篇文章里小編給大家整理的是一篇關(guān)于vue父組件給子組件的組件傳值provide inject的方法,需要的朋友們學(xué)習(xí)下。2019-10-10
vue在install時(shí)node-sass@4.14.1?postinstall:node?scripts/buil
最近在npm install 的時(shí)候遇到了個(gè)問題,所以給大家總結(jié)下,下面這篇文章主要給大家介紹了關(guān)于vue在install時(shí)node-sass@4.14.1?postinstall:node?scripts/build.js錯(cuò)誤的解決方法,需要的朋友可以參考下2023-05-05
說說如何在Vue.js中實(shí)現(xiàn)數(shù)字輸入組件的方法
這篇文章主要介紹了說說如何在Vue.js中實(shí)現(xiàn)數(shù)字輸入組件的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
vue使用WebSocket模擬實(shí)現(xiàn)聊天功能
這篇文章主要介紹了vue使用WebSocket模擬實(shí)現(xiàn)聊天功能,通過創(chuàng)建node服務(wù)和server.js文件實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
element-ui?el-upload實(shí)現(xiàn)上傳文件及簡單的上傳文件格式驗(yàn)證功能
前端上傳文件后,后端接受文件進(jìn)行處理后直接返回處理后的文件,前端直接再將文件下載下來,下面這篇文章主要給大家介紹了關(guān)于element-ui?el-upload實(shí)現(xiàn)上傳文件及簡單的上傳文件格式驗(yàn)證功能的相關(guān)資料,需要的朋友可以參考下2022-11-11

