參考vue-router官方文檔的 路由懶加載 部分html
const Login = () => Promise.resolve({ /* 組件定義對象 */ })
import('./Login.vue') // 返回 Promise
const Login = () => import('./login.vue')
const Login = () => import(/* webpackChunkName: "Login" */ './login.vue')
const routers = new Router({ routes: [ { path: '/login', name: 'login', component: Login }, ...
【注意】webpack.base.conf.js 文件中的 output 修改,影響的是本地開發環境運行的chunk文件的名稱,【注意】webpack.prod.conf.js 中的修改,影響 經過 npm run build 打包後的chunk文件的名稱。vue
module.exports = { entry: { app: './src/main.js' }, output: { path: config.build.assetsRoot, filename: '[name].js', // 打包後的文件名 1.js, ...
module.exports = { entry: { app: './src/main.js' }, output: { path: config.build.assetsRoot, filename: '[name].[hash].js', chunkFilename: '[name].chunk.[hash].js', //打包後的文件名 Login.chunk.2f8e6a3b7cd6aaa0d7ba.js ...
To sum up, make sure you set: comments: true in .babalrc (this is the default) chunkFilename: '[name].....' in your webpack config If you are using typescript, also make sure: removeComments: false under compilerOptions in tsconfig.json module: esnext in tsconfig.json
{ "comments": false, ...
若是您使用的是 Babel,你將須要添加 syntax-dynamic-import 插件,才能使 Babel 能夠正確地解析語法。 插件地址
npm install --save-dev @babel/plugin-syntax-dynamic-import
{ "plugins": ["@babel/plugin-syntax-dynamic-import"] }