vue内置组件keep-alive多级路由缓存最佳实践

在我们的业务中,我们常常会有列表页跳转详情页,详情页可能还会继续跳转下一级页面,当我们返回上一级页面时,我想保持前一次的所有查询条件以及页面的当前状态。一想到页面缓存,在vue中我们就想到keep-alive这个vue的内置组件,在keep-alive这个内置组件提供了一个include的接口,只要路由name匹配上就会缓存当前组件。你或多或少看到不少很多处理这种业务代码,本文是一篇笔者关于缓存多页面的解决实践方案,希望看完在业务中有所思考和帮助。

正文开始…

业务目标

首先我们需要确定需求,假设A是列表页,A-1是详情页,A-1-1,A-1-2是详情页的子级页面,B是其他路由页面

我们用一个图来梳理一下需求

大概就是这样的,一图胜千言

然后我们开始,主页面大概就是下面这样

pages/list/index.vue我们暂且把这个当成A页面模块吧

<template><div class="list-app"><div><a href="javascript:void(0)" @click="handleToHello">to hello</a></div><el-form ref="form" :model="condition" label-width="80px" inline><el-form-item label="姓名"><el-inputv-model="condition.name"clearableplaceholder="请输入搜索姓名"></el-input></el-form-item><el-form-item label="地址"><el-select v-model="condition.address" placeholder="请选择地址"><el-optionv-for="item in tableData":key="item.name":label="item.address":value="item.address"></el-option></el-select></el-form-item><el-form-item><el-button @click="featchList">刷新</el-button></el-form-item></el-form><el-table:data="tableData"style="width: 100%"row-key="id"borderlazy:load="load":tree-props="{ children: 'children', hasChildren: 'hasChildren' }"><el-table-column prop="date" label="日期"> </el-table-column><el-table-column prop="name" label="姓名"> </el-table-column><el-table-column prop="address" label="地址"> </el-table-column><el-table-column prop="options" label="操作"><template slot-scope="scope"><a href="javascript:void(0);" @click="handleView">查看详情</a><a href="javascript:void(0);" @click="handleEdit(scope.row)">编辑</a></template></el-table-column></el-table><!--分页--><el-pagination@current-change="handleChangePage"backgroundlayout="prev, pager, next":total="100"></el-pagination><!--弹框--><list-modaltitle="编辑"width="50%"v-model="formParams":visible.sync="dialogVisible"@refresh="featchList"></list-modal></div>
</template> 

我们再看下对应页面的业务js

<!--pages/list/index.vue-->
<script> import { sourceDataMock } from '@/mock';
import ListModal from './ListModal';

export default {name: 'list',components: {ListModal,},data() {retur
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 keep-alive 多级路由缓存,需要在父级路由中使用 keep-alive 组件,并在子级路由中设置 name 属性,以便 keep-alive 组件可以缓存子级路由对应的组件实例。同时,在子级路由对应的组件中也要添加 keep-alive 组件,以缓存组件的状态。 下面是一个示例代码: ```javascript // 父级路由设置 <template> <div> <keep-alive> <router-view v-if="$route.meta.keepAlive" /> </keep-alive> <router-view v-if="!$route.meta.keepAlive" /> </div> </template> <script> export default { name: 'Parent', components: {}, data() { return {}; }, computed: {}, methods: {} }; </script> // 子级路由设置 <template> <div> <keep-alive :include="cachedViews"> <router-view /> </keep-alive> </div> </template> <script> export default { name: 'Child', components: {}, data() { return {}; }, computed: { cachedViews() { const { meta } = this.$route; return meta && meta.keepAlive ? [meta.keepAlive] : []; } }, methods: {} }; </script> // 路由配置 const router = new VueRouter({ routes: [ { path: '/', name: 'Parent', component: Parent, meta: { keepAlive: 'Parent' }, children: [ { path: '/child1', name: 'Child1', component: Child1, meta: { keepAlive: 'Child1' }, children: [ { path: '/grandchild1', name: 'Grandchild1', component: Grandchild1, meta: { keepAlive: 'Grandchild1' } }, { path: '/grandchild2', name: 'Grandchild2', component: Grandchild2, meta: { keepAlive: 'Grandchild2' } } ] }, { path: '/child2', name: 'Child2', component: Child2, meta: { keepAlive: 'Child2' } } ] } ] }); ``` 在这个示例中,Parent 组件作为父级路由组件,使用了 keep-alive 组件缓存子级路由对应的组件实例。而 Child 组件作为子级路由组件,则使用了自己的 keep-alive 组件缓存组件的状态。为了让 keep-alive 组件知道要缓存哪个组件,我们在 meta 属性中添加了一个 keepAlive 字段,用来标识组件的名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值