vue router按需加载
import Vue from 'vue'
import Router from 'vue-router' Vue.use(Router);
//按需加载,当渲染其他页面时才加载其组件,并缓存,减少首屏加载时间
const Index = resolve => require(['@/views/Index.vue'], resolve)
const Category = resolve => require(['@/views/Category.vue'], resolve)
const CategoryMain = resolve => require(['@/components/category/main.vue'], resolve)
const Car = resolve => require(['@/views/Car.vue'],resolve)
const User = resolve => require(['@/views/User.vue'], resolve)
const Detail = resolve => require(['@/views/Detail.vue'], resolve)
const Search = resolve => require(['@/views/Search.vue'], resolve)
const Pay = resolve => require(['@/components/car/pay/pay.vue'], resolve)
const Login = resolve => require(['@/views/login.vue'], resolve) export default new Router({
routes: [{
path: '/',
name: '首页',
component: Index
}, {
path: '/category',
name: '分类页',
redirect: '/category/all',
component: Category,
children: [{
path: '/category/:tab',
component:CategoryMain
}]
}, {
path: '/car',
name: '购物车页',
component: Car
}, {
path: '/car/pay',
name: '支付页',
component: Pay
},
{
path: '/user',
name: '用户页',
component: User,
meta: {
requireAuth: true, // 添加该字段,表示进入这个路由是需要登录才能进入的
},
}, {
path: '/detail',
name: '详情页',
component: Detail
}, {
path: '/search',
name: '搜索页',
component: Search
},{
path: '/login',
name: '登录页',
component: Login
}
]
})
import Vue from 'vue'
import Router from 'vue-router' // import Index from '@/components/index/index'
// import Find from '@/components/find/find'
// import Order from '@/components/order/order'
// import Mine from '@/components/mine/mine'
// import RestaurantDetail from '@/components/index/restaurant-detail/restaurant-detail'
// import RestaurantList from '@/components/index/restaurant-list/restaurant-list'
// import Goods from '@/components/index/restaurant-detail/goods/goods'
// import Ratings from '@/components/index/restaurant-detail/ratings/ratings'
// import Seller from '@/components/index/restaurant-detail/seller/seller'
// import login from '@/components/login/login' Vue.use(Router) // 路由懒加载
const Index = (resolve) => {
import('@/components/index/index').then((module) => {
resolve(module)
})
}
const Find = (resolve) => {
import('@/components/find/find').then((module) => {
resolve(module)
})
}
const Order = (resolve) => {
import('@/components/order/order').then((module) => {
resolve(module)
})
}
const Mine = (resolve) => {
import('@/components/mine/mine').then((module) => {
resolve(module)
})
}
const RestaurantDetail = (resolve) => {
import('@/components/index/restaurant-detail/restaurant-detail').then((module) => {
resolve(module)
})
}
const RestaurantList = (resolve) => {
import('@/components/index/restaurant-list/restaurant-list').then((module) => {
resolve(module)
})
}
const Goods = (resolve) => {
import('@/components/index/restaurant-detail/goods/goods').then((module) => {
resolve(module)
})
}
const Ratings = (resolve) => {
import('@/components/index/restaurant-detail/ratings/ratings').then((module) => {
resolve(module)
})
}
const Seller = (resolve) => {
import('@/components/index/restaurant-detail/seller/seller').then((module) => {
resolve(module)
})
}
const login = (resolve) => {
import('@/components/login/login').then((module) => {
resolve(module)
})
} export default new Router({
routes: [
// 根路径
{
path: '/',
redirect: '/index',
component: Index
},
// 首页
{
path: '/index',
component: Index
},
// 登录
{
path: '/login',
component: login
},
// 商家列表
{
path: '/restaurant_list',
component: RestaurantList
},
// 商家模块
{
path: '/restaurant',
redirect: '/restaurant/goods',
component: RestaurantDetail,
children: [
{
path: 'goods',
component: Goods
},
{
path: 'ratings',
component: Ratings
},
{
path: 'seller',
component: Seller
}
]
},
// 发现
{
path: '/find',
component: Find
},
// 订单
{
path: '/order',
component: Order
},
// 我的
{
path: '/mine',
component: Mine
}
]
})
vue router按需加载的更多相关文章
- 深入浅出的webpack4构建工具---webpack+vue+router 按需加载页面(十五)
1. 为什么需要按需加载? 对于vue单页应用来讲,我们常见的做法把页面上所有的代码都打包到一个bundle.js文件内,但是随着项目越来越大,文件越来越多的情况下,那么bundle.js文件也会越来 ...
- 原创《分享(Angular 和 Vue)按需加载的项目实践优化方案》
针对前端优化的点有很多,例如:图片压缩,雪碧图,js/css/html 文件的压缩合并, cdn缓存, 减少重定向, 按需加载 等等 最近有心想针对 ionic项目 和 vue项目,做一个比较大的优 ...
- Vue Router的懒加载路径
单页应用产出的入口chunk大小随着业务的复杂度线性增加,导致后期加载速度越来越慢.后面就需要对不同路径下的模块进行拆分,打包到相应的chunk下,按需加载,找到chunk的大小.个数和页面加载速度的 ...
- vue项目按需加载的3种方式
本文重要是路由打包优化: 原理:利用webpack对代码进行分割是懒加载的前提,懒加载就是异步调用组件,需要时候才下载. 1.vue异步组件技术 vue-router配置路由,使用vue的异步组件技术 ...
- React Router 按需加载+服务器渲染的闪屏问题
伴随着React协议的『妥协』(v16采用MIT),React为项目的主体,这个在短期内是不会改变的了,在平时使用过程中发现了如下这个问题: 在服务器渲染的时候,刷新页面会出现闪屏的现象(白屏一闪而过 ...
- vue实现按需加载(懒加载)
1.router文件中使用 export default new Router({ routes: [{ path: '/', name: 'Post', component: () => im ...
- Vue按需加载提升用户体验
Vue官方文档异步组件: 在大型应用中,我们可能需要将应用拆分为多个小模块,按需从服务器下载.为了让事情更简单, Vue.js 允许将组件定义为一个工厂函数,动态地解析组件的定义.Vue.js 只在组 ...
- vue单页面打包文件大?首次加载慢?按需加载?是你打开方式不对
部署各vue项目,走了一遍坑.... vue单页面应用刷新404 找到nginx多网站配置文件:类似nginx/sites-available/www.baidu.com server { liste ...
- vue中路由按需加载的几种方式
使用vue-cli构建项目后,我们会在Router文件夹下面的index.js里面引入相关的路由组件,如: import Hello from '@/components/Hello' import ...
随机推荐
- Linux中的自定义变量
范围: 只在当前Shell中生效 语法: 变量定义: name="xiaol" hello="$name"" hello" 变量调用: ec ...
- 判断数A和数B中有多少个位不相同
1. A & B,得到的结果C中的1的位表明了A和B中相同的位都是1的位:2. A | B, 得到的结果D中的1的位表明了A和B在该位至少有一个为1的位,包含了A 与 B 都是1的位数,经过前 ...
- oracle 函数 截取 连接 替换 判断
一个处理不规范日期的函数,廖记一下吧,以免再忘. --注意全角半角 CREATE OR REPLACE function f_str2form( date_string in varchar2 ) r ...
- 【转】通过fio工具,测试SATA,SAS,SSD 读写性能
转自:http://blog.csdn.net/killmice/article/details/42745937
- Vuex mapGetters,mapActions
一.基本用法 1. 初始化并创建一个项目 ? 1 2 3 vue init webpack-simple vuex-demo cd vuex-demo npm install 2. 安装 vuex ? ...
- java基础—— Collections.sort的两种用法
package com.jabberchina.test; import java.util.ArrayList; import java.util.Collections; import java. ...
- OC_NSString、
8月17日NSString 一.字符串的创建 //1.创建常量字符串 NSString *str1 = @"hello world"; NSLog(@"%@", ...
- ubuntu+vm+ftp
为了将windows下的文件传到linux中去,使用FZ来做服务器,在linux中进入ftp状态获取. 1.下载FileZilla服务器,在windows下安装就行了(试过汉化插件,用了就报错,所以还 ...
- 关于form表单中button按钮自动提交问题
坑:点击确认按钮,form表单提交2次,发送后台2次请求 //错误代码: <Button id="btnSubmit" name="btnSubmit" ...
- Android电容屏(一)【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/7820492 关键词:Android 电容屏 tp ITO 平台信息:内核:linu ...