您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Vue keep alive如何使用”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
路由(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>
代碼
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。
代碼
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)。
代碼
修改路由設(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。
修改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)。
說(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/#/
“Vue keep alive如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。