vue解决使用webpack打包后keep-alive不生效

本文介绍了一个关于Webpack中keep-alive缓存机制的问题,即在开发环境中该机制正常工作,但在生产环境中遇到缓存失效的情况。通过调整路由组件的导入方式,解决了缓存不生效的问题。

问题是这样的,我使用webpack的npm run dev运行的时候,keep-alive路由缓存是有效的,但是我npm run build,把文件放到实际的项目中去的时候,会有如下的问题:

路由如下:

var menus = [
  {
    path: '/user',
    name: '用户',
    component: '/user',
    redirect: '/user/index1',
    icon: 'fa-bandcamp',
    meta: {
      keepAlive: false
    },
    children: [
      { path: 'index1', component: '/user/index1', name: '用户管理1', meta: {keepAlive: true}},
      { path: 'index2', component: '/user/index2', name: '用户管理2', meta: {keepAlive: true}},
    ]
  },{
    path: '/system',
    name: 'system',
    component: '/system',
    redirect: '/system/index',
    icon: 'fa-superpowers',
    noDropdown: true,
    meta: {
      keepAlive: false
    },
    children: [
      { path: 'index', component: '/system/index', name: '系统管理', meta: {keepAlive: true}},
    ]
  },
]

我在 /user/index1/user/index2 之间做切换的时候路由缓存还是生效的,但是在user和system之间切换的时候就不生效了,解决方法如下,也解释不清楚什么原因,可能生产环境下需要在路由的文件上面做缓存把

在route目录下新建两个文件:

_import_development.js

module.exports = file => require('@/views/' + file + '.vue')

_import_production.js

module.exports = file => () => import('@/views/' + file + '.vue')

原先路由import的时候是这样的:

import Home from '@/views/home/homeView'

现在改成这样:

const _import = require('./_import_' + process.env.NODE_ENV);
const Login = _import('index/loginView')

重新打包运行生效

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值