02.vue-router的进阶使用
关键字:路由懒加载,全局导航守卫,组件导航守卫,redirect重定向,keep-alive,params,query
一、目录结构

二、index.js
// 配置路由相关的信息
import VueRouter from 'vue-router'
import Vue from 'vue' // 一般加载方法
// import Home from '../components/Home'
// import About from '../components/About'
// import User from '../components/User' // 路由懒加载方法(ES6)
const Home = () => import('../components/Home');
const HomeNews = () => import("../components/HomeNews");
const HomeMessage = () => import("../components/HomeMessage"); const About = () => import("../components/About");
const User = () => import('../components/User');
const Profile = () => import('../components/Profile'); // 1.通过Vue.use(插件), 安装插件
Vue.use(VueRouter); // 2.创建VueRouter对象
const routes = [
{
path: '',
// redirect重定向(到默认页面)
// redirect: '/home'
redirect: '/home',
meta:{
title: '首页'
}
},
{
path: '/home',
component: Home,
meta : {
title: '首页'
},
children: [
{
path: '',
redirect: "news",
},
{
path: 'news',
component: HomeNews,
},
{
path: "message",
component: HomeMessage,
},
]
},
{
path: '/about',
component: About,
meta: {
title: "关于"
}
},
{
path: '/user/:userId',
component: User,
meta:{
title: '用户' }
},
{
path: '/profile',
component: Profile,
meta: {
title: '档案'
}
}
];
const router = new VueRouter({
// 配置路由和组件之间的应用关系
routes,
// mode: 'history',默认是hash
mode: 'history',
// linkActiveClass: 'active'
// 统一简化class-active成active
linkActiveClass: "active"
}); // guard (守卫,警卫)
// 全局导航守卫,传一个函数
// 前置守卫(也叫前置钩子hook)
router.beforeEach((to,from,next) => {
// 从from路由跳到to路由
// to就是meta的对象
document.title = to.matched[0].meta.title;
console.log("前置导航守卫");
// 原先会自动执行next(),但我们现在重新创建了beforeEach,所以必须手动执行下next(),
next()
}); // 全局后置钩子
router.afterEach(()=>{
console.log("后置导航守卫");
}); // 3.将router对象传入到Vue实例
export default router
三、main.js
import Vue from 'vue'
import App from './App'
import router from './router' Vue.config.productionTip = false; // 全局Vue类下定义一个实例,可全局访问
Vue.prototype.Test001 = function () {
console.log("Test001")
};
Vue.prototype.Name001 = "zwnsyw"; new Vue({
el: '#app',
router,
render: h => h(App)
})
四、App.vue
<template>
<div id="app">
<h2>我是APP组件</h2>
<!-- tag(定义渲染成什么标签)、replace(浏览器返回按钮不可用) 、active-class(活跃时添加class类active)-->
<!--<router-link to="/home" tag="button" replace active-class="active">首页</router-link>-->
<!--<router-link to="/about" tag="button" replace active-class="active">关于</router-link>-->
<!--<router-link to="/home" tag="button" replace>首页</router-link>-->
<!--<router-link to="/about" tag="button" replace>关于</router-link>-->
<!-- <button @click="homeClick">首页</button>-->
<!-- <button @click="aboutClick">关于</button>-->
<router-link to="/home" tag="button" replace>首页</router-link>
<router-link to="/about" tag="button" replace>关于</router-link>
<router-link v-bind:to="'/user/'+userId">用户</router-link>
<!-- <router-link :to="{path: '/profile',query:{name: 'zwnsyw', age: 18, height: 1.88}}">档案</router-link>-->
<button @click="profileClick">档案</button> <!-- 保持活跃状态,不要每次都重新创建-->
<!-- excloud是排除某些控件,让这些控件频繁的创建和销毁,两个之间不可加空格-->
<keep-alive exclude="Profile,User">
<router-view></router-view>
</keep-alive> </div>
</template> <script>
export default {
name: 'App',
data(){
return {
userId: "lisi",
img: 'http'
}
},
methods: {
homeClick() {
// 通过代码的方式修改路由 vue-router
// push => pushState
// this.$router.push('/home')
this.$router.replace('/home')
console.log('homeClick');
},
aboutClick() {
// this.$router.push('/about')
this.$router.replace('/about')
// this.$router.push('/about')
console.log('aboutClick');
},
profileClick(){
this.$router.push({
path: '/profile',
query: {
name: 'kobe',
age: '45',
height: '1.98'
}
})
}
}
}
</script> <style>
/*.router-link-active {*/
/*color: #f00;*/
/*}*/ .active {
color: #f00;
}
</style>
五、Home.vue
<template>
<div>
<h2>我是首页</h2>
<p>我是首页内容, 哈哈哈</p>
<router-link to="/home/news">新闻</router-link>
<router-link to="/home/message">消息</router-link>
<router-view></router-view>
</div>
</template> <script>
export default {
name: "Home",
data(){
return{
path: "/home/news"
}
},
// 这两个函数,只有该组件被保持了状态,使用了keep-alive时,才是有效的
// 组件活跃的时候回调
activated() {
this.$router.push(this.path);
console.log("activated");
}, // 组件不活跃,销毁的时候回调
deactivated(){
console.log("deactivated");
},
// 组件导航守卫
beforeRouteLeave(to , from, next){
this.path = this.$route.path;
next()
},
} </script> <style scoped> </style>
<template>
<div>
<ul>
<li>消息1</li>
<li>消息2</li>
<li>消息3</li>
<li>消息4</li>
</ul>
</div>
</template> <script>
export default {
name: "HomeMessage"
}
</script> <style scoped> </style>
HomeMessage
<template>
<div>
<!-- ul>li{新闻$}*4-->
<ul>
<li>新闻1</li>
<li>新闻2</li>
<li>新闻3</li>
<li>新闻4</li>
</ul>
<button @click="btn_test">访问自定义全局实例</button>
</div>
</template> <script>
export default {
name: "Homenews",
methods:{
btn_test(){
console.log(this.Test001());
console.log(this.Name001)
}
}
}
</script> <style scoped> </style>
Homenews
六、About.vue
<template>
<div>
<h2>我是关于</h2>
<p>我是关于内容, 呵呵呵</p>
</div>
</template> <script>
export default {
name: "About"
}
</script> <style scoped> </style>
七、User.vue
<template>
<div>
<h2>我是用户</h2>
<p>我是用户内容,嘿嘿嘿</p>
<h2>{{userId}}</h2>
<h2>{{$route.params.userId}}</h2>
</div>
</template> <script>
export default {
name: "User",
computed:{
userId(){ // 所有的组件都继承于Vue类的原型 // route 拿到的是当前活跃的路由对象
// router 拿到的是index.js里面new出来的总路由对象
return this.$route.params.userId // parameters(参数)
}
}
}
</script> <style scoped> </style>
八、Profile.vue
<template>
<div>
<h2>我是Profile组件</h2>
<h2>{{$route.query}}</h2>
<h2>{{$route.query.name}}</h2>
<h2>{{$route.query.age}}</h2>
<h2>{{$route.query.height}}</h2>
</div>
</template> <script>
export default {
name: "Profile"
}
</script> <style scoped> </style>
02.vue-router的进阶使用的更多相关文章
- 「进阶篇」Vue Router 核心原理解析
前言 此篇为进阶篇,希望读者有 Vue.js,Vue Router 的使用经验,并对 Vue.js 核心原理有简单了解: 不会大篇幅手撕源码,会贴最核心的源码,对应的官方仓库源码地址会放到超上,可以配 ...
- vue Router——进阶篇
vue Router--基础篇 1.导航守卫 正如其名,vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的 ...
- Vue 2.0 + Vue Router + Vuex
用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...
- vue router 只需要这么几步
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Vue.js 2.x笔记:路由Vue Router(6)
1. Vue Router简介与安装 1.1 Vue Router简介 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,构建单页面应用. Vue Rout ...
- Vue Router学习笔记
前端的路由:一个地址对应一个组件 Vue Router中文文档 一.路由基本使用 第1步:导入Vue Router: <script src="https://unpkg.com/vu ...
- vue router.push(),router.replace(),router.go()和router.replace后需要返回两次的问题
转载:https://www.cnblogs.com/lwwen/p/7245083.html https://blog.csdn.net/qq_15385627/article/details/83 ...
- 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI
一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...
- 深入浅出的webpack4构建工具---webpack+vue+router 按需加载页面(十五)
1. 为什么需要按需加载? 对于vue单页应用来讲,我们常见的做法把页面上所有的代码都打包到一个bundle.js文件内,但是随着项目越来越大,文件越来越多的情况下,那么bundle.js文件也会越来 ...
- 深入浅出的webpack构建工具--webpack4+vue+router项目架构(十四)
阅读目录 一:vue-router是什么? 二:vue-router的实现原理 三:vue-router使用及代码配置 四:理解vue设置路由导航的两种方法. 五:理解动态路由和命名视图 六:理解嵌套 ...
随机推荐
- Unity3D的UGUI布局锚点自动绑定关系
[MenuItem("CONTEXT/RectTransform/Auto")] public static void AutoRectAnior() { Debug.Log(&q ...
- ORACLE重做日志小结
1.Redo log特点 重做日志以磁盘I/O为主,将数据库操作记录到日志文件.(磁盘I\O性能有可能成为瓶颈) 每个实例只有一个活动的LGWR(log writer)进程,至少有两个日志组(logf ...
- Django的ListView超详细用法(含分页paginate功能)
开发环境: python 3.6 django 1.11 场景一 经常有从数据库中获取一批数据,然后在前端以列表的形式展现,比如:获取到所有的用户,然后在用户列表页面展示. 解决方案 常规写法是,我们 ...
- React Native 架构演进
写在前面 上一篇(React Native 架构一览)从设计.线程模型等方面介绍了 React Native 的现有架构,本篇将分析这种架构的局限性,以及 React Native 正在进行的架构升级 ...
- search(16)- elastic4s-内嵌文件:nested and join
从SQL领域来的用户,对于ES的文件关系维护方式会感到很不习惯.毕竟,ES是分布式数据库只能高效处理独个扁平类型文件,无法支持关系式数据库那样的文件拼接.但是,任何数据库应用都无法避免树型文件关系,因 ...
- Java——java.lang.NullPointerException
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = ...
- Java——java.lang.NumberFormatException: For input string: ""
java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.f ...
- linux克隆之后网络设置
1.修改网络 vi /etc/sysconfig/network-scripts/ifcfg-eth0 修改:ip地址 IPADDR=192.168.77.83GATEWAY=192.168.77.2 ...
- Javascript输入输出语句
方法 说明 归属 alert(msg) 浏览器弹出警示框 浏览器 console.log(msg) 浏览器控制台打印输出信息 浏览器 prompt(info) 浏览器弹出输入框,用户可以输入 浏览器 ...
- MySQL死锁系列-常见加锁场景分析
在上一篇文章<锁的类型以及加锁原理>主要总结了 MySQL 锁的类型和模式以及基本的加锁原理,今天我们就从原理走向实战,分析常见 SQL 语句的加锁场景.了解了这几种场景,相信小伙伴们也能 ...