【Vue】Re17 Router 第四部分(参数传递,守卫函数)
一、案例搭建
新建Profile组件

组件写好内容后配置路由
{
path : '/profile',
component : () => import('../components/Profile')
}
二、参数配置
App.vue配置profile
我们可以使用对象对to的url进行封装
path属性就是url
query属性就是我们的请求数据【给地址的请求参数】
<template>
<div id="app">
<router-link to="/home" tag="button" >去首页</router-link>
<router-link to="/about" tag="button" >去关于</router-link>
<router-link :to="/user/+username" tag="button" >用户管理</router-link>
<router-link :to="ppp" tag="button" >profile</router-link>
<!-- <button @click="toHome">首页</button>-->
<!-- <button @click="toAbout">关于</button>-->
<router-view></router-view>
</div>
</template> <script>
export default {
name: 'App',
data() {
return {
username : 'aaa',
ppp : {
path : '/profile',
query : {
name : 'aaa',
age : 22,
gender : true
}
}
}
}
// methods : {
// toHome () {
// this.$router.push('/home');
// },
// toAbout () {
// this.$router.push('/about');
// }
// }
}
</script> <style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
可以看到参数这样传递过来了

如果要取出请求参数,则可以这样:
写在方法里面获取,记得先从this对象引用
<template>
<div>
<h3>Profile-Component</h3>
<p>
profile component content ... <br>
name -> {{$route.query.name}} <br>
age -> {{$route.query.age}} <br>
gender -> {{$route.query.gender}}
</p>
</div>
</template> <script>
export default {
name: "Profile"
}
</script> <style scoped> </style>
路由的代码写法:
<template>
<div id="app">
<router-link to="/home" tag="button" >去首页</router-link>
<router-link to="/about" tag="button" >去关于</router-link>
<router-link :to="/user/+username" tag="button" >用户管理</router-link>
<!-- <router-link :to="ppp" tag="button" >profile</router-link>-->
<button @click="toProfile" >profile</button>
<!-- <button @click="toHome">首页</button>-->
<!-- <button @click="toAbout">关于</button>-->
<router-view></router-view>
</div>
</template> <script>
export default {
name: 'App',
data() {
return {
username : 'aaa',
ppp : {
path : '/profile',
query : {
name : 'aaa',
age : 22,
gender : true
}
}
}
},
methods : {
toHome () {
this.$router.push('/home');
},
toAbout () {
this.$router.push('/about');
},
toProfile() {
this.$router.push(this.ppp);
}
} }
</script> <style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
三、$route & $router的区别?
router是全局路由控制器对象
route是当前活跃的路由对象,是routes属性数组中的一个元素,也可以是router对象内部的一个属性对象
https://www.bilibili.com/video/BV15741177Eh?p=113
四、导航守卫方法
1、回顾Vue实例的生命周期
created() {
// todo ... Vue创建时调用此函数,
},
mounted() {
// todo ... Vue实例开始挂载虚拟DOM时调用此函数
},
updated() {
// todo ... ... 组件数据发生更新时调用
},
点击不同的子组件,更换标签文本的需求:
每一次访问不同的组件都会调用created函数
所以可以:
created() {
// todo ... Vue创建时调用此函数,
document.title = '关于-About';
},
但是这样N个组件就要写N次了,都是路由跳转的方式进行的
那么只要在监听路由跳转,在那个时刻把title组件的某一个数据就行了
import Vue from 'vue';
import Router from 'vue-router'; Vue.use(Router); /* 方式一 */
const Home = resolve => {
require.ensure(
['../components/Home.vue'],
() => resolve(require('../components/Home.vue'))
)
}
/* 方式二 */
const About = resolve => {
require(['../components/About.vue'],resolve);
}
/* 方式三 */
const User = () => import('../components/User.vue'); const News = () => import('../components/home/News');
const Messages = () => import('../components/home/Messages'); const routerList = [
/* 重定向首页路由配置 */
{
path : '', /* 缺省值默认指向 '/' */
redirect : '/home',
},
{
path : '/home', /* 为什么这里是path不是url? 因为完整的url还包括 项目根url(协议头 + 域名(或者IP地址) + 端口号 + 项目根名称路径(可选)) */
name : 'home', /* 名字可以不加 */
component : Home, /* 使用懒加载后component这里高亮显示 */
children : [ /* 设置子路由 */
{
path : '', /* 这个缺省默认/home */
redirect : 'news',
},
{
path : 'news', /* 等同于 /home + /news = /home/news 这里不需要再加斜杠了 */
component : News,
meta : {
title : '新闻列表 - News'
}
},
{
path : 'messages',
component : Messages,
meta : {
title : '消息列表 - Messages'
}
}
],
meta : {
title : '首页 - Home'
}
},
{
path : '/about',
name : 'about',
component : About,
meta : {
title : '关于 - About'
}
},
{
path : '/user/:username', /* 动态路径:冒号+字符 */
name : 'user',
component : User,
meta : {
title : '用户 - User'
}
},
{
path : '/profile',
component : () => import('../components/Profile'),
meta : {
title : '档案 - Profile'
}
}
]
const router = new Router({
routes : routerList,
mode : 'history',
}); /* 在创建实例后调用 */
router.beforeEach((to, from, next) => {
// 调用这个方法以为着重写,一定要调用 next方法, 否则路由无法跳转 // from 来自于哪个路由对象
// to 跳转到哪个路由对象 // 按照案例的需求,就可以这样设置了 route就是一个个routes的元素对象
// 可以设置一个meta属性对象,放入title属性和对应的值即可
document.title = to.meta.title; // 但是子路由没有命名的话会早曾undefined显示,因为meta属性为空
// 解决方案 document.title = to.matched[0].meta.title; // 跳转要放在最后,不然是跳完了再执行标签的文本更换
next();
});
export default router;
afterEach守卫方法:
router.afterEach((to, from) => {
// 因为是跳转之后调用,自然而然的不需要next函数了
// TODO ... ...
});
上述的两个都是全局守卫方法,在路由配置中重写并调用
五、局部路由守卫
详细资料:
https://router.vuejs.org/guide/advanced/navigation-guards.html
You can define beforeEnter guards directly on a route's configuration object:
const router = new VueRouter({
routes: [
{
path: '/foo',
component: Foo,
beforeEnter: (to, from, next) => {
// ...
}
}
]
})
In-Component Guards
Finally, you can directly define route navigation guards inside route components (the ones passed to the router configuration) with the following options:
beforeRouteEnterbeforeRouteUpdatebeforeRouteLeave
const Foo = {
template: `...`,
beforeRouteEnter (to, from, next) {
// called before the route that renders this component is confirmed.
// does NOT have access to `this` component instance,
// because it has not been created yet when this guard is called!
},
beforeRouteUpdate (to, from, next) {
// called when the route that renders this component has changed.
// This component being reused (by using an explicit `key`) in the new route or not doesn't change anything.
// For example, for a route with dynamic params `/foo/:id`, when we
// navigate between `/foo/1` and `/foo/2`, the same `Foo` component instance
// will be reused (unless you provided a `key` to `<router-view>`), and this hook will be called when that happens.
// has access to `this` component instance.
},
beforeRouteLeave (to, from, next) {
// called when the route that renders this component is about to
// be navigated away from.
// has access to `this` component instance.
}
}
【Vue】Re17 Router 第四部分(参数传递,守卫函数)的更多相关文章
- 四 Vue学习 router学习
index.js: 按需加载组件: const login = r => require.ensure([], () => r(require('@/page/login')), 'log ...
- vue之router钩子函数
模块一:全局导航钩子函数 1.vue router.beforeEach(全局前置守卫) beforeEach的钩子函数,它是一个全局的before 钩子函数, (before each)意思是在 每 ...
- Vue的路由动态重定向和导航守卫
一.重定向 重定向也是通过 routes 配置来完成,下面例子是从 /a 重定向到 /b: const router = new VueRouter({ routes: [ { path: '/a', ...
- 详解vue 路由跳转四种方式 (带参数)
详解vue 路由跳转四种方式 (带参数):https://www.jb51.net/article/160401.htm 1. router-link ? 1 2 3 4 5 6 7 8 9 10 ...
- Vue学习笔记-Vue.js-2.X 学习(四)===>脚手架Vue-CLI(基本工作和创建)
(五) 脚手架Vue-CLI 一 Vue-CLI前提(nodejs和webpack) 二 Vue学习-nodejs按装配置,Node.js 就是运行在服务端的 JavaScript. 1. 去nod ...
- 三、vue之router
三.vue之router 此时vue的脚手架.创建项目已经完成. ... vue的运行流程 index.html-->main.js-->App.vue-->router/index ...
- Vue中router两种传参方式
Vue中router两种传参方式 1.Vue中router使用query传参 相关Html: <!DOCTYPE html> <html lang="en"> ...
- Vue基础系列(四)——Vue中的指令(上)
写在前面的话: 文章是个人学习过程中的总结,为方便以后回头在学习. 文章中会参考官方文档和其他的一些文章,示例均为亲自编写和实践,若有写的不对的地方欢迎大家和我一起交流. VUE基础系列目录 < ...
- vue 中router.go;router.push和router.replace的区别
vue 中router.go:router.push和router.replace的区别:https://blog.csdn.net/div_ma/article/details/79467165 t ...
- 【vue】 router.beforeEach
import store from '@/store' const Vue = require('vue') const Router = require('vue-router') Vue.use( ...
随机推荐
- vue单个插槽
当子组件模板只有一个没有属性的插槽时,父组件传入的整个内容片段将插入到插槽所在的 DOM 位置,并替换掉插槽标签本身. # 子组件 <div> <h2>我是子组件的标题< ...
- kettle从入门到精通 第三十一课 mysql 数据连接连接池配置
无论开发应用程序还是做ETL研发,都离不开连接池的应用,如下是kettle中mysql 连接池设置界面,今天重点讲解下连接池中的参数配置. defaultAutoCommit 当 defaultAut ...
- CF914C
problem & blog 数位 dp 模板题. 经过一次操作,可以把 \(n\) 变成一个小于 \(10^3\) 的数. 所以我们可以把所有小于 \(10^3\) 的数操作的次数全部处理出 ...
- 国产搜索引擎崛起:Elasticsearch 国产化加速
背景 多年来,Elasticsearch(简称:ES) 在搜索领域一直独占鳌头,其卓越的性能和广泛的应用深受国内众多企业的青睐.从查询搜索到数据分析,再到安全分析,Elasticsearch 均展现出 ...
- 手摸手教你把Ingress Nginx集成进Skywalking
背景 在微服务大行其道的今天,如何观测众多微服务.快速理清服务间的依赖.如何对服务之间的调用性能进行衡量,成了摆在大家面前的难题.对此,Skywalking应运而生,它是托管在 Apache 基金会下 ...
- SQL 如何去掉字段中千位的逗号(比如set @= '1,320.00' 想得到@= '1320.00' )
1/去掉字段里的逗号.(比如set @= '1,320.00' 想得到@= '1320.00' )UPDATE table SET fieldA = REPLACE(fieldA, ',', '') ...
- word文档生成视频,自动配音、背景音乐、自动字幕,另类创作工具
简介 不同于别的视频创作工具,这个工具创作视频只需要在word文档中打字,插入图片即可.完事后就能获得一个带有配音.字幕.背景音乐.视频特效滤镜的优美作品. 这种不要门槛,没有技术难度的视频创作工具, ...
- zabbix---监控Oracle12c数据库
使用插件:orabbix用于监控oracle实例的zabbix插件 orabbix插件下载地址:http://www.smartmarmot.com/product/orabbix/download/ ...
- mysql的varchar和oracle的varchar2比较
首先说结论: 1.mysql存储的是字符数(不分语言) 2.oracle存储的需要看定义,如果定义为varchar2(n),则默认是n个字节,如果是varchar2(n char)则是n个字节. 3. ...
- python 第一次主要是if while 奇偶数的练习
例如输出1-10的数字,但是7除外. a=1 while a < 11: if a == 7: pass else: print(a) a=a+1 输出1-100所以的偶数 a=1 while ...