关键字:路由懒加载,全局导航守卫,组件导航守卫,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. 【雕爷学编程】Arduino动手做(46)---电阻应变片传感器

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...

  2. redis搭建实录

    #!/bin/bash####redis版本为4.2.0,需要php5.6以上才支持,可先将安装包上传到/tools目录. yum -y install wgetyum -y install unzi ...

  3. 【1-n】区间覆盖 TOJ4168+BZOJ1192

    Xiao Ming is very interesting for array. He given a sorted positive integer array and an integer n. ...

  4. Java操作XML(1)

    XML简介 XML(Extensible Markup Language)是一种非常有用的迷哦书结构化信息的技术.XML工具使处理和转化信息变得十分容易. 在程序中,可以使用属性文件(property ...

  5. Understanding REST and RESTful APIs

    Understanding REST and RESTful APIs If you've spent any amount of time with modern web development, ...

  6. 允许远程用户登录访问mysql

    方法1.本地登入mysql,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,将”localhost”改为”%” mysql -u root -proot use mysql; ...

  7. ngnix随笔四

    1.alias path 例1. =>http://www.a.com/bbs/ root /data/vhosts/; location /bbs/{ alias /data/a.com/; ...

  8. This关键字练习

    Account: package com.aff.ex; public class Account { private int id;// 账号 private double balance;// 余 ...

  9. centos6.4中文输入法

    在虚拟机中装了centos6.4之后,一直使用命令行,没有用到编辑器编辑中文或者浏览器中文搜索,所以没有注意到里边中文输入的重要性.在网上有看到说如果用的是中文版本应该有自带的中文输入法,然后用快捷键 ...

  10. 50个SQL语句(MySQL版) 问题十一

    --------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 tea ...