关键字:路由懒加载,全局导航守卫,组件导航守卫,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的进阶使用的更多相关文章

  1. 「进阶篇」Vue Router 核心原理解析

    前言 此篇为进阶篇,希望读者有 Vue.js,Vue Router 的使用经验,并对 Vue.js 核心原理有简单了解: 不会大篇幅手撕源码,会贴最核心的源码,对应的官方仓库源码地址会放到超上,可以配 ...

  2. vue Router——进阶篇

    vue Router--基础篇 1.导航守卫 正如其名,vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的 ...

  3. Vue 2.0 + Vue Router + Vuex

    用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...

  4. vue router 只需要这么几步

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. Vue.js 2.x笔记:路由Vue Router(6)

    1. Vue Router简介与安装 1.1 Vue Router简介 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,构建单页面应用. Vue Rout ...

  6. Vue Router学习笔记

    前端的路由:一个地址对应一个组件 Vue Router中文文档 一.路由基本使用 第1步:导入Vue Router: <script src="https://unpkg.com/vu ...

  7. 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 ...

  8. 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI

    一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...

  9. 深入浅出的webpack4构建工具---webpack+vue+router 按需加载页面(十五)

    1. 为什么需要按需加载? 对于vue单页应用来讲,我们常见的做法把页面上所有的代码都打包到一个bundle.js文件内,但是随着项目越来越大,文件越来越多的情况下,那么bundle.js文件也会越来 ...

  10. 深入浅出的webpack构建工具--webpack4+vue+router项目架构(十四)

    阅读目录 一:vue-router是什么? 二:vue-router的实现原理 三:vue-router使用及代码配置 四:理解vue设置路由导航的两种方法. 五:理解动态路由和命名视图 六:理解嵌套 ...

随机推荐

  1. flex布局学习总结--阮一峰

    基本概念:   采用 Flex 布局的元素,称为 Flex 容器(flex container),简称"容器".它的所有子元素自动成为容器成员,称为 Flex 项目(flex it ...

  2. node的http模块

    node中的几个常用核心模块的api返回的都是eventEmitter的实例,也就是说都继承了on和emit方法,用以监听事件并触发回调来处理事件. http模块处理网络请求通常是创建一个server ...

  3. 【遗传编程/基因规划】Genetic Programming

    目录 背景介绍 程序表示 初始化 (Initialization) Depth定义 Grow方法 Full方法 Ramped half-and-half方法 适应度(Fitness)与选择(Selec ...

  4. DPDK LPM库(学习笔记)

    1 LPM库 DPDK LPM库组件为32位的key实现了最长前缀匹配(LPM)表查找方法,该方法通常用于在IP转发应用程序中找到最佳路由匹配. 2 LPM API概述 LPM组件实例的主要配置参数是 ...

  5. hdu2243

    背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了.一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如"ab",放在单词前一般表 ...

  6. Gym101630L Laminar Family

    题目链接:https://cn.vjudge.net/problem/Gym-101630L 题目大意: 对于一个集合的集合,若其中任意两个集合 \(A\) 和 \(B\) 都满足下述三个条件之一:\ ...

  7. Flask搭建个人博客网站(1)—项目规划--李渣渣(lizaza.cn)

    Flask搭建个人博客网站(1)—项目规划--李渣渣(lizaza.cn) 发布时间:2020-05-2413次浏览 前言 现在市面上又许多比较成熟的博客平台,例如:CSDN,博客园,新浪博客等!对于 ...

  8. mysql新

    .数据库服务器:运行数据库管理软件的计算机 .数据库管理软件:MySQL,oracle,db2,sqlserver .库:文件夹 .表:文件 .记录:事物的一系列典型特征:name,age,schoo ...

  9. MIT6.828准备:MacOS下搭建xv6和risc-v环境

    本文介绍在MacOS下搭建Mit6.828/6.S081 fall2019实验环境的详细过程,包括riscv工具链.qemu和xv6,对于Linux系统同样可以参考. 介绍 只有了解底层原理才能写好上 ...

  10. 【JUC】synchronizated和lock的区别&新lock的优势

    原始构成 synchronized是关键字,属于JVM层面 javap -c 的结果显示 synchronized是可重入锁 11:是正常退出 17:是异常退出[保证不产生死锁和底层故障] Lock是 ...