关键字:路由懒加载,全局导航守卫,组件导航守卫,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. c++简单string实现

    string.h #pragma once class string { public: string(const char* str = nullptr); string(const string& ...

  2. mysql运维入门2:主从架构

    mysql主从原理 随着访问量的增加,数据库压力的增加,需要对msyql进行优化和架构改造,优化方法有: 高可用 主从复制 读写分离 拆分库 拆分表 原理 异步复制过程 master开启bin-log ...

  3. 容器技术之Docker网络

    上一篇博客我们主要聊了下docker镜像相关的说明以及怎样基于现有镜像制作镜像.分发镜像到docker仓库中的相关测试:回顾请参考https://www.cnblogs.com/qiuhom-1874 ...

  4. LNMP PHP升级脚本

    升级PHP前,请确认你的网站程序是否支持升级到的PHP版本,防止升级到网站程序不兼容的PHP版本,具体可以去你使用的PHP程序的官网查询相关版本支持信息.v1.3及以后版本大部分情况下也可以进行降级操 ...

  5. protected和private的区别

    1. protected和private在没有继承关系的类A和类B之间其作用都可以视为式一样的--表示私有--每个类中的protected字段/属性都不能被访问到: 2. 当类与类之间存在继承关系时候 ...

  6. sql 索引常见失效的几种情况

    1. 对于联合索引,没有遵循左前缀原则 2. 索引的字段区分度不大,可能引起索引近乎全表扫描 3. 对于join操作,索引字段的编码不一致,导致使用索引失效 4.对于hash索引,范围查询失效,has ...

  7. UWP开发入门(25)——通过Radio控制Bluetooth, WiFi

    回顾写了许久的UWP开发入门,竟然没有讲过通过Windows.Devices.Radios.Radio来控制Bluetooth和WiFi等功能的开关.也许是因为相关的API设计的简单好用,以至于被我给 ...

  8. AVIRIS反射率数据简介

    Surface Reflectance 高光谱图像 ↑ AVIRIS高光谱成像光谱仪采集得到的原始图像为辐亮度图像,经过校正后的L1级产品为地表辐亮度信息.但是许多时候,我们更希望知道地面目标物的反射 ...

  9. java中的垃圾处理机制

    1.何为垃圾在Java中,如果对象实体没有引用指向的话,存储该实体的内存便成为垃圾.JVM会有一个系统线程专门负责回收垃圾.垃圾同时包括分配对象内存间的碎片块 2.垃圾处理包含的算法 Java语言规范 ...

  10. Rocket - util - IDPool

    https://mp.weixin.qq.com/s/Pe7FGKzfRufzzYDrl0fQ7g   介绍IDPool的实现.   ​​   1. 基本介绍   实现从ID池中分配和释放ID的功能. ...