• Vue--keep-alive--使用/实例


    原文网址:Vue--keep-alive--使用/实例_IT利刃出鞘的博客-CSDN博客

    简介

    说明

            本文用示例介绍Vue的keep-alive的用法。

            keep-alive标签主要用于保留组件状态或避免重新渲染。

    官网网址

    https://v2.cn.vuejs.org/v2/api/#keep-alive

    相关网址

    Vue--keep-alive--详解_IT利刃出鞘的博客-CSDN博客

    公共代码

    路由(router/index.js)

    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. import Parent from '../components/Parent'
    4. Vue.use(VueRouter)
    5. const routes = [
    6. {
    7. path: '/',
    8. name: 'Parent',
    9. component: Parent
    10. }
    11. ]
    12. const router = new VueRouter({
    13. routes
    14. })
    15. export default router

    生命周期mixin(mixins/LifeCycle.js)

    下边的两个页面组件都要打印生命周期,所以我把它抽出作为mixin

    1. export default {
    2. computed: {
    3. name () {
    4. return this.$options.name
    5. }
    6. },
    7. created () {
    8. console.log('created ==> ' + this.name)
    9. },
    10. activated () {
    11. console.log('activated ==> ' + this.name)
    12. },
    13. deactivated () {
    14. console.log('deactivated ==> ' + this.name)
    15. },
    16. destroyed () {
    17. console.log('destroyed ==> ' + this.name)
    18. }
    19. }

    子页面组件

    components/CompA.vue

    1. <template>
    2. <div class="outer">
    3. <div>
    4. 这是CompA
    5. div>
    6. div>
    7. template>
    8. <script>
    9. import LifeCycle from '../mixins/LifeCycle'
    10. export default {
    11. name: 'CompA',
    12. mixins: [LifeCycle]
    13. }
    14. script>
    15. <style scoped>
    16. .outer {
    17. margin: 20px;
    18. border: 2px solid blue;
    19. padding: 20px;
    20. }
    21. style>

    components/CompB.vue 

    1. <template>
    2. <div class="outer">
    3. 这是CompB
    4. div>
    5. template>
    6. <script>
    7. import LifeCycle from '../mixins/LifeCycle'
    8. export default {
    9. name: 'CompB',
    10. mixins: [LifeCycle]
    11. }
    12. script>
    13. <style scoped>
    14. .outer {
    15. margin: 20px;
    16. border: 2px solid blue;
    17. padding: 20px;
    18. }
    19. style>

    动态组件实例

    不加keep-alive

    代码

    components/Parent.vue

    1. <template>
    2. <div class="outer">
    3. <div>
    4. 这是Parent
    5. div>
    6. <br>
    7. <button @click="useCompA">切换到CompAbutton>
    8. |
    9. <button @click="useCompB">切换到CompBbutton>
    10. <component :is="componentName">component>
    11. div>
    12. template>
    13. <script>
    14. import CompA from './CompA'
    15. import CompB from './CompB'
    16. export default {
    17. name: 'Parent',
    18. components: {
    19. CompA,
    20. CompB
    21. },
    22. data () {
    23. return {
    24. componentName: ''
    25. }
    26. },
    27. methods: {
    28. useCompA () {
    29. this.componentName = 'CompA'
    30. },
    31. useCompB () {
    32. this.componentName = 'CompB'
    33. }
    34. }
    35. }
    36. script>
    37. <style scoped>
    38. .outer {
    39. margin: 20px;
    40. border: 2px solid red;
    41. padding: 20px;
    42. }
    43. style>

    测试

    访问:http://localhost:8080/#/

    可以发现:进入组件时调用created;离开组件时调用destroyed;再进入组件时调用created

    加keep-alive

    代码

    component/Parent.vue

    将动态组件用keep-alive标签包起来。

    1. <template>
    2. <div class="outer">
    3. <div>
    4. 这是Parent
    5. div>
    6. <br>
    7. <button @click="useCompA">切换到CompAbutton>
    8. |
    9. <button @click="useCompB">切换到CompBbutton>
    10. <keep-alive>
    11. <component :is="componentName">component>
    12. keep-alive>
    13. div>
    14. template>
    15. <script>
    16. import CompA from './CompA'
    17. import CompB from './CompB'
    18. export default {
    19. name: 'Parent',
    20. components: {
    21. CompA,
    22. CompB
    23. },
    24. data () {
    25. return {
    26. componentName: ''
    27. }
    28. },
    29. methods: {
    30. useCompA () {
    31. this.componentName = 'CompA'
    32. },
    33. useCompB () {
    34. this.componentName = 'CompB'
    35. }
    36. }
    37. }
    38. script>
    39. <style scoped>
    40. .outer {
    41. margin: 20px;
    42. border: 2px solid red;
    43. padding: 20px;
    44. }
    45. style>

    测试

    访问:http://localhost:8080/#/

     

    可以发现:进入组件时调用created、activated;离开组件时调用deactivated(不调用destroyed)。

    router-view实例

    不加keep-alive

    代码

    修改路由设置(router/index.js)

    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. import Parent from '../components/Parent'
    4. import CompA from '../components/CompA'
    5. import CompB from '../components/CompB'
    6. Vue.use(VueRouter)
    7. const routes = [
    8. {
    9. path: '/',
    10. name: 'Parent',
    11. component: Parent
    12. },
    13. {
    14. path: '/compA',
    15. name: 'CompA',
    16. component: CompA
    17. },
    18. {
    19. path: '/compB',
    20. name: 'CompB',
    21. component: CompB
    22. }
    23. ]
    24. const router = new VueRouter({
    25. routes
    26. })
    27. export default router

    修改入口组件(components/Parent.vue)

    1. <template>
    2. <div class="outer">
    3. <div>
    4. 这是Parent
    5. div>
    6. <br>
    7. <router-link :to="{name: 'CompA'}">跳转到CompArouter-link>
    8. |
    9. <router-link :to="{name: 'CompB'}">跳转到CompBrouter-link>
    10. <router-view>router-view>
    11. div>
    12. template>
    13. <script>
    14. export default {
    15. name: 'Parent'
    16. }
    17. script>
    18. <style scoped>
    19. .outer {
    20. margin: 20px;
    21. border: 2px solid red;
    22. padding: 20px;
    23. }
    24. style>

    测试

    访问:http://localhost:8080/#/

    可以发现:进入路由组件时调用created;离开路由组件时调用destroyed。

    加keep-alive(加到Parent.vue里)

    router-link+嵌套路由

    说明

    如果是使用router-link切换路由,需要做如下两步:

    1. Parent.vue:将router-view包裹在keep-alive里边
    2. 将CompA和CompB的路由作为Parent的嵌套路由(Children)

            如果只修改Parent.vue,将router-view包裹在keep-alive里边,这样是没用的,跟没有加keep-alive效果一样。

    代码

    修改components/Parent.vue

    1. <template>
    2. <div class="outer">
    3. <div>
    4. 这是Parent
    5. div>
    6. <br>
    7. <router-link :to="{name: 'CompA'}">跳转到CompArouter-link>
    8. |
    9. <router-link :to="{name: 'CompB'}">跳转到CompBrouter-link>
    10. <keep-alive>
    11. <router-view>router-view>
    12. keep-alive>
    13. div>
    14. template>
    15. <script>
    16. export default {
    17. name: 'Parent'
    18. }
    19. script>
    20. <style scoped>
    21. .outer {
    22. margin: 20px;
    23. border: 2px solid red;
    24. padding: 20px;
    25. }
    26. style>

    修改路由(router/index.js) 

    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. import Parent from '../components/Parent'
    4. import CompA from '../components/CompA'
    5. import CompB from '../components/CompB'
    6. Vue.use(VueRouter)
    7. const routes = [
    8. {
    9. path: '/',
    10. name: 'Parent',
    11. component: Parent,
    12. children: [
    13. {
    14. path: '/compA',
    15. name: 'CompA',
    16. component: CompA
    17. },
    18. {
    19. path: '/compB',
    20. name: 'CompB',
    21. component: CompB
    22. }
    23. ]
    24. }
    25. ]
    26. const router = new VueRouter({
    27. routes
    28. })
    29. export default router

    测试

    访问:http://localhost:8080/#/

            可以发现:进入组件时调用created、activated;离开组件时调用deactivated(不调用destroyed);再进入组件时调用activated(不调用created)。

    非router-link(无需嵌套路由)

    说明

    如果没有使用router-link切换路由,只需要做:

    1. Parent.vue:将router-view包裹在keep-alive里边

    但这样要注意一点:组件之间进行切换时,路由不会改变!

    代码

    router/index.js

    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. import Parent from '../components/Parent'
    4. Vue.use(VueRouter)
    5. const routes = [
    6. {
    7. path: '/',
    8. name: 'Parent',
    9. component: Parent
    10. }
    11. ]
    12. const router = new VueRouter({
    13. routes
    14. })
    15. export default router

    components/Parent.vue

    1. <template>
    2. <div class="outer">
    3. <div>
    4. 这是Parent
    5. div>
    6. <br>
    7. <button @click="useCompA">切换到CompAbutton>
    8. |
    9. <button @click="useCompB">切换到CompBbutton>
    10. <keep-alive>
    11. <component :is="componentName">component>
    12. keep-alive>
    13. div>
    14. template>
    15. <script>
    16. import CompA from './CompA'
    17. import CompB from './CompB'
    18. export default {
    19. name: 'Parent',
    20. components: {
    21. CompA,
    22. CompB
    23. },
    24. data () {
    25. return {
    26. componentName: ''
    27. }
    28. },
    29. methods: {
    30. useCompA () {
    31. this.componentName = 'CompA'
    32. },
    33. useCompB () {
    34. this.componentName = 'CompB'
    35. }
    36. }
    37. }
    38. script>
    39. <style scoped>
    40. .outer {
    41. margin: 20px;
    42. border: 2px solid red;
    43. padding: 20px;
    44. }
    45. style>

    测试

    访问 :http://localhost:8080/#/

            可以发现:进入组件时调用created、activated;离开组件时调用deactivated(不调用destroyed);再进入组件时调用activated(不调用created)。

    加keep-alive(加到App.vue里)

    修改App.vue

    将router-view标签包裹在keep-alive标签里。

    1. <template>
    2. <div id="app">
    3. <div id="nav">
    4. <router-link to="/">Homerouter-link>
    5. |
    6. <router-link to="/about">Aboutrouter-link>
    7. div>
    8. <keep-alive>
    9. <router-view/>
    10. keep-alive>
    11. div>
    12. template>
    13. <style>
    14. style>

    测试

    访问:http://localhost:8080/#/

            可以发现:进入组件时调用created、activated;离开组件时调用deactivated(不调用destroyed);再进入组件时调用activated(不调用created)。

  • 相关阅读:
    java教师教学质量评估系统计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    针对海量数据的存储与访问瓶颈的解决方案
    RAKsmart洛杉矶大带宽服务器支持哪些操作系统?
    【Golang】数组 && 切片
    Ubuntu20运行SegNeXt代码提取道路水体(五)——使用SegNeXt跑自己的数据集结果及分析
    Python技法:用argparse模块解析命令行选项
    【数据结构】二叉树 -- 堆
    【新版本来袭】ONLYOFFICE桌面编辑器8.1 —— 重塑办公效率与体验
    scss文件自动导入
    【图形学】27 透明度混合
  • 原文地址:https://blog.csdn.net/feiying0canglang/article/details/126293945