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 ...
随机推荐
- jQuery 属性操作
1 css操作 2 文本操作 3 属性操作 4 位置 5 尺寸 1.css操作 addClass();// 添加指定的CSS类名. removeClass();// 移除指定的CSS类名. hasCl ...
- 免费 SSL 安全证书
为了保证网上传输信息的安全而在自己的 Linode VPS 上部署 SSL 加密服务.商业 CA 较贵,所以使用了自己签发的 CA.网友神爱的留言提到了 StartSSL 的免费 CA,稍做了一些调查 ...
- Python学习笔记2_Python基础
一.变量(给数据起个名字) 变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变. 1.变量的命名方法 -变量名有字母.数字.下划线组成 -不能以数字开头 -不可以使用关键字 -a ...
- R&python机器学习之朴素贝叶斯分类
朴素贝叶斯算法描述应用贝叶斯定理进行分类的一个简单应用.这里之所以称之为“朴素”,是因为它假设各个特征属性是无关的,而现实情况往往不是如此. 贝叶斯定理也称贝叶斯推理,早在18世纪,英国学者贝叶斯(1 ...
- django自带的用户认证和form表单功能
一.用户认证 1.用户认证方法 1.ajango自带用户认证功能,只需要引入相应的模块就可以使用,但是前提是必须使用ajango自带的auth_user表,并且需要把用户相关信息存放在该表中. 2.引 ...
- c# 抽象类(abstract)
using System; using System.Collections.Generic; using System.Linq; using System.Text; //抽象类(abstract ...
- 每天一个Linux命令(63)scp命令
scp(secure copy)用于进行远程文件拷贝. (1)用法: 用法: scp [参数] [源文件] [目标文件] (2)功能: 功能: scp在主机 ...
- wifidog源码分析 - wifidog原理 tiger
转:http://www.cnblogs.com/tolimit/p/4223644.html wifidog源码分析 - wifidog原理 wifidog是一个用于配合认证服务器实现无线网页认证功 ...
- 使用jsp+javabean完成用户登陆功能
User.java package com.po; public class User implements java.io.Serializable { private String usernam ...
- 登陆weblogic后页面控制台卡主
输入http://localhost:7001/console进入控制页面,能登陆进去,但是登陆进去后页面就马上卡死,可以看到页面头部,其余都显示不出来. 重启后启动访问,能够正常进入,关闭weblo ...