博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue + vue-router 懒加载 import / resolve+require
阅读量:6452 次
发布时间:2019-06-23

本文共 2971 字,大约阅读时间需要 9 分钟。

hot3.png

1、webpack 配置:build/webpack.base.conf.js

module.export = {  ...  output: {    path: config.build.assetsRoot,    filename: '[name].js',    chunkFilename: '[name].js',// 添加这一行 name 可以根据 webpackChunkName 注释友好定义    publicPath: process.env.NODE_ENV === 'production' ?      config.build.assetsPublicPath :      config.dev.assetsPublicPath  },}

2、代码示例:

import 版本,可定义 webpackChunkName 注解,我喜欢

如果报错 import 方法不存在的话

cnpm install -D babel-plugin-syntax-dynamic-import//在.bablerc中引入syntax-dynamic-import插件{    "presets": ["es2015", "stage-3"],    "plugins": ["syntax-dynamic-import"]}

 

import Vue from 'vue'import VueRouter from 'vue-router'// 页面组件 webpackChunkName 注释可以友好化分包的名称 需 webpack 2.4+// 使用 require + resolve 可以获得更好的通用性 但不支持 webpackChunkName 注解 用 id 做感觉可读性不好const componentMarket    = () => import( /* webpackChunkName: "market" */ '@/components/Market');const componentCandy     = () => import( /* webpackChunkName: "candy" */ '@/components/Candy');const componentWhiteBook = () => import( /* webpackChunkName: "white-book" */ '@/components/WhiteBook');const componentLogin     = () => import( /* webpackChunkName: "login" */ '@/components/Auth/Login');const componentRegister  = () => import( /* webpackChunkName: "register" */ '@/components/Auth/Register');Vue.use(VueRouter)const router = new VueRouter({  mode: 'history',  routes: [{      path: '/',      name: '/market',      component: componentMarket    },    {      path: '/candy',      name: '/candy',      component: componentCandy    },    {      path: '/white-book',      name: '/white-book',      component: componentWhiteBook    },    {      path: '/login',      name: '/login',      component: componentLogin    },    {      path: '/register',      name: '/register',      component: componentRegister    }  ]})

resolve + require 版本,使用 id 做 chunkName

// import Market from '@/components/Market'// import Candy from '@/components/Candy'// import WhiteBook from '@/components/WhiteBook'// import Login from '@/components/Auth/Login'// import Register from '@/components/Auth/Register'import Vue from 'vue'import VueRouter from 'vue-router'Vue.use(VueRouter)const router = new VueRouter({  mode: 'history',  routes: [{      path: '/',      name: '/market',      component: resolve => require(['@/components/Market'], resolve)    },    {      path: '/candy',      name: '/candy',      component: resolve => require(['@/components/Candy'], resolve)    },    {      path: '/white-book',      name: '/white-book',      component: resolve => require(['@/components/WhiteBook'], resolve)    },    {      path: '/login',      name: '/login',      component: resolve => require(['@/components/Auth/Login'], resolve)    },    {      path: '/register',      name: '/register',      component: resolve => require(['@/components/Auth/Register'], resolve)    }  ]})router.beforeEach((to, from, next) => {  console.log(to);  console.log(from);  next();})export default router;

就ok了

转载于:https://my.oschina.net/sallency/blog/2353973

你可能感兴趣的文章
git bash 风格调整
查看>>
997D Cycles in product
查看>>
bzoj4589 Hard Nim
查看>>
java实现pdf旋转_基于Java实现PDF文本旋转倾斜
查看>>
java二维数组内存模型_C++二级指针第二种内存模型(二维数组)
查看>>
java static import 与 import_Java中的import和static import语句之间有什么区别?
查看>>
python time库3.8_python3中datetime库,time库以及pandas中的时间函数区别与详解
查看>>
java 代替Python_Java总是“沉沉浮浮”,替代者会是Python?
查看>>
贪吃蛇java程序简化版_JAVA简版贪吃蛇
查看>>
poi java web_WebPOI JavaWeb 项目 导出excel表格(.xls) Develop 238万源代码下载- www.pudn.com...
查看>>
java 顶点着色_金属顶点着色器绘制纹理点
查看>>
php扩展有哪些G11,php 几个扩展(extension)的安装笔记
查看>>
ajax长连接 php,ajax怎么实现服务器与浏览器长连接
查看>>
oracle报1405,【案例】Oracle报错ORA-15054 asm diskgroup无法mount的解决办法
查看>>
php 5.4.24 win32,PHP 5.4.14 和 PHP 5.3.24 发布
查看>>
oracle top pid,Linux Top 命令解析 比较详细
查看>>
grub如何进入linux系统,Linux操作系统启动管理器-GRUB
查看>>
linux pbs 用户时间,【Linux】单计算机安装PBS系统(Torque)与运维
查看>>
linux系统可用内存减少,在Linux中检查可用内存的5种方法
查看>>
linux 脚本map,Linux Shell Map的用法详解
查看>>