溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

vue使用webpack打包后keep-alive不生效怎么辦

發布時間:2021-07-13 14:02:13 來源:億速云 閱讀:152 作者:小新 欄目:web開發

這篇文章將為大家詳細講解有關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')

重新打包運行生效!

關于“vue使用webpack打包后keep-alive不生效怎么辦”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女