vue解决使用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')

重新打包运行生效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值