Vue--keep-alive使用實(shí)例詳解
簡(jiǎn)介
說(shuō)明
本文用示例介紹Vue的keep-alive的用法。
keep-alive標(biāo)簽主要用于保留組件狀態(tài)或避免重新渲染。
官網(wǎng)網(wǎng)址
https://v2.cn.vuejs.org/v2/api/#keep-alive
相關(guān)網(wǎng)址
vue2中的keep-alive使用總結(jié)及注意事項(xiàng)
公共代碼
路由(router/index.js)
import Vue from 'vue' import VueRouter from 'vue-router' import Parent from '../components/Parent' Vue.use(VueRouter) const routes = [ { path: '/', name: 'Parent', component: Parent } ] const router = new VueRouter({ routes }) export default router
生命周期mixin(mixins/LifeCycle.js)
下邊的兩個(gè)頁(yè)面組件都要打印生命周期,所以我把它抽出作為mixin。
export default { computed: { name () { return this.$options.name } }, created () { console.log('created ==> ' + this.name) }, activated () { console.log('activated ==> ' + this.name) }, deactivated () { console.log('deactivated ==> ' + this.name) }, destroyed () { console.log('destroyed ==> ' + this.name) } }
子頁(yè)面組件
components/CompA.vue
<template> <div class="outer"> <div> 這是CompA </div> </div> </template> <script> import LifeCycle from '../mixins/LifeCycle' export default { name: 'CompA', mixins: [LifeCycle] } </script> <style scoped> .outer { margin: 20px; border: 2px solid blue; padding: 20px; } </style>
components/CompB.vue
<template> <div class="outer"> 這是CompB </div> </template> <script> import LifeCycle from '../mixins/LifeCycle' export default { name: 'CompB', mixins: [LifeCycle] } </script> <style scoped> .outer { margin: 20px; border: 2px solid blue; padding: 20px; } </style>
動(dòng)態(tài)組件實(shí)例
不加keep-alive
代碼
components/Parent.vue
<template> <div class="outer"> <div> 這是Parent </div> <br> <button @click="useCompA">切換到CompA</button> | <button @click="useCompB">切換到CompB</button> <component :is="componentName"></component> </div> </template> <script> import CompA from './CompA' import CompB from './CompB' export default { name: 'Parent', components: { CompA, CompB }, data () { return { componentName: '' } }, methods: { useCompA () { this.componentName = 'CompA' }, useCompB () { this.componentName = 'CompB' } } } </script> <style scoped> .outer { margin: 20px; border: 2px solid red; padding: 20px; } </style>
測(cè)試
訪問(wèn):http://localhost:8080/#/
可以發(fā)現(xiàn):進(jìn)入組件時(shí)調(diào)用created;離開組件時(shí)調(diào)用destroyed。
加keep-alive
代碼
component/Parent.vue
將動(dòng)態(tài)組件用keep-alive標(biāo)簽包起來(lái)。
<template> <div class="outer"> <div> 這是Parent </div> <br> <button @click="useCompA">切換到CompA</button> | <button @click="useCompB">切換到CompB</button> <keep-alive> <component :is="componentName"></component> </keep-alive> </div> </template> <script> import CompA from './CompA' import CompB from './CompB' export default { name: 'Parent', components: { CompA, CompB }, data () { return { componentName: '' } }, methods: { useCompA () { this.componentName = 'CompA' }, useCompB () { this.componentName = 'CompB' } } } </script> <style scoped> .outer { margin: 20px; border: 2px solid red; padding: 20px; } </style>
測(cè)試
訪問(wèn):http://localhost:8080/#/
可以發(fā)現(xiàn):進(jìn)入組件時(shí)調(diào)用created、activated;離開組件時(shí)調(diào)用deactivated(不調(diào)用destroyed)。
router-view實(shí)例
不加keep-alive
代碼
修改路由設(shè)置(router/index.js)
import Vue from 'vue' import VueRouter from 'vue-router' import Parent from '../components/Parent' import CompA from '../components/CompA' import CompB from '../components/CompB' Vue.use(VueRouter) const routes = [ { path: '/', name: 'Parent', component: Parent }, { path: '/compA', name: 'CompA', component: CompA }, { path: '/compB', name: 'CompB', component: CompB } ] const router = new VueRouter({ routes }) export default router
修改入口組件(components/Parent.vue)
<template> <div class="outer"> <div> 這是Parent </div> <br> <router-link :to="{name: 'CompA'}">跳轉(zhuǎn)到CompA</router-link> | <router-link :to="{name: 'CompB'}">跳轉(zhuǎn)到CompB</router-link> <router-view></router-view> </div> </template> <script> export default { name: 'Parent' } </script> <style scoped> .outer { margin: 20px; border: 2px solid red; padding: 20px; } </style>
測(cè)試
訪問(wèn):http://localhost:8080/#/
可以發(fā)現(xiàn):進(jìn)入路由組件時(shí)調(diào)用created;離開路由組件時(shí)調(diào)用destroyed。
加keep-alive(加到App.vue里)
修改App.vue
將router-view標(biāo)簽包裹在keep-alive標(biāo)簽里。
<template> <div id="app"> <div id="nav"> <router-link to="/">Home</router-link> | <router-link to="/about">About</router-link> </div> <!-- 原來(lái)寫法 --> <!-- <router-view/> --> <keep-alive> <router-view/> </keep-alive> </div> </template> <style> <!-- 省略 --> </style>
測(cè)試
訪問(wèn):http://localhost:8080/#/
可以發(fā)現(xiàn):進(jìn)入組件時(shí)調(diào)用created、activated;離開組件時(shí)調(diào)用deactivated(不調(diào)用destroyed)。
加keep-alive(加到Parent.vue里)
說(shuō)明
需要做如下兩步:
- Parent.vue:將router-view包裹在keep-alive里邊
- 將CompA和CompB的路由作為Parent的嵌套路由(Children)
如果只修改Parent.vue,將router-view包裹在keep-alive里邊,這樣是沒用的,跟沒有加keep-alive效果一樣。
代碼
修改components/Parent.vue
<template> <div class="outer"> <div> 這是Parent </div> <br> <router-link :to="{name: 'CompA'}">跳轉(zhuǎn)到CompA</router-link> | <router-link :to="{name: 'CompB'}">跳轉(zhuǎn)到CompB</router-link> <keep-alive> <router-view></router-view> </keep-alive> </div> </template> <script> export default { name: 'Parent' } </script> <style scoped> .outer { margin: 20px; border: 2px solid red; padding: 20px; } </style>
修改路由(router/index.js)
import Vue from 'vue' import VueRouter from 'vue-router' import Parent from '../components/Parent' import CompA from '../components/CompA' import CompB from '../components/CompB' Vue.use(VueRouter) const routes = [ { path: '/', name: 'Parent', component: Parent, children: [ { path: '/compA', name: 'CompA', component: CompA }, { path: '/compB', name: 'CompB', component: CompB } ] } ] const router = new VueRouter({ routes }) export default router
測(cè)試
訪問(wèn):http://localhost:8080/#/
到此這篇關(guān)于Vue--keep-alive使用實(shí)例詳解的文章就介紹到這了,更多相關(guān)Vue--keep-alive使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue使用keep-alive如何實(shí)現(xiàn)多頁(yè)簽并支持強(qiáng)制刷新
- vue中keep-alive組件實(shí)現(xiàn)多級(jí)嵌套路由的緩存
- vue使用keep-alive后清除緩存的方法
- Vue中keep-alive 實(shí)現(xiàn)后退不刷新并保持滾動(dòng)位置
- vue keep-alive列表頁(yè)緩存 詳情頁(yè)返回上一頁(yè)不刷新,定位到之前位置
- vue組件 keep-alive 和 transition 使用詳解
- vue使用keep-alive保持滾動(dòng)條位置的實(shí)現(xiàn)方法
- vue里如何主動(dòng)銷毀keep-alive緩存的組件
- vue2.x中keep-alive源碼解析(實(shí)例代碼)
相關(guān)文章
element?select必填項(xiàng)驗(yàn)證回顯問(wèn)題的解決
本文主要介紹了element?select必填項(xiàng)驗(yàn)證回顯問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04vue 實(shí)現(xiàn)setInterval 創(chuàng)建和銷毀實(shí)例
這篇文章主要介紹了vue 實(shí)現(xiàn)setInterval 創(chuàng)建和銷毀實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07基于ant-design-vue實(shí)現(xiàn)表格操作按鈕組件
這篇文章主要為大家介紹了基于ant-design-vue實(shí)現(xiàn)表格操作按鈕組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06elementui?el-upload一次請(qǐng)求上傳多個(gè)文件的實(shí)現(xiàn)
使用ElementUI?Upload的時(shí)候發(fā)現(xiàn)如果是默認(rèn)方案,上傳多張圖片并不是真正的一次上傳多張,本文就來(lái)介紹一下elementui?el-upload一次請(qǐng)求上傳多個(gè)文件的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10vue實(shí)現(xiàn)短信驗(yàn)證碼登錄功能(流程詳解)
無(wú)論是移動(dòng)端還是pc端登錄或者注冊(cè)界面都會(huì)見到手機(jī)驗(yàn)證碼登錄這個(gè)功能,輸入手機(jī)號(hào),得到驗(yàn)證碼,這篇文章主要介紹了基于vue實(shí)現(xiàn)短信驗(yàn)證碼登錄功能,需要的朋友可以參考下2019-12-12解決vue打包之后靜態(tài)資源圖片失效的問(wèn)題
下面小編就為大家分享一篇解決vue打包之后靜態(tài)資源圖片失效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02vue項(xiàng)目刷新當(dāng)前頁(yè)面的三種方式(重載當(dāng)前頁(yè)面數(shù)據(jù))
這篇文章主要介紹了vue項(xiàng)目刷新當(dāng)前頁(yè)面的三種方式(重載當(dāng)前頁(yè)面數(shù)據(jù)),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01淺談將three項(xiàng)目遷移至vue項(xiàng)目遇到的問(wèn)題
本文主要介紹了將three項(xiàng)目遷移至vue項(xiàng)目遇到的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01