- 路由按需加载,按模块名称。
const login = () => import(/* webpackChunkName: "login" */'./components/login'); const router = new Router({ // mode: 'history', //去掉url中的# routes: [ { path: '/', name: 'login', label: '登录', component: login }, ] })
2.组件中引入组件按需加载
const baseInfo = () => import('./baseInfo'); export default { name: 'main', components: { baseInfo, }, data(){ return:{ page:"", } }, mounted() {}, methods:{ } }
3.vue.config.js配置去掉预加载文件。
module.exports = {
chainWebpack:(config)=>{
config.plugin('preload-index').tap(() => [
{
rel: 'preload',
fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
include: 'initial'
}
]);
config.plugins.delete('preload-index');
config.plugins.delete('prefetch-index');
config.plugins.delete("prefetch");//只加载当前页面需要的js
}
}