路由
简介
路由在Vue项目开发中较为广泛,复杂项目中多个页面间的跳转都需要用到路由。
什么是路由

前端路由及后端路由


使用步骤



一级路由配置

示例
<html>
<head>
<meta charset="UTF-8">
<title>一级路由</title>
<!--导入Vue及路由-->
<script type="text/javascript" src="js/vue-2.4.0.js" ></script>
<script type="text/javascript" src="js/vue-router-3.0.1.js" ></script>
</head>
<body>
<div id="app">
<!--编写路由链接: 类似a标签,作为1个跳转热点内容-->
<router-link to="/login">login</router-link>
<router-link to="/zhuce">zhuce</router-link>
<!--创建路由出口: 专门用于展示路由资源的位置-->
<router-view></router-view>
</div>
<!--模板-->
<template id="logintp">
<h1>登录</h1>
</template>
<template id="zhucetp">
<h1>注册</h1>
</template>
<script type="text/javascript">
//创建路由对象: 路由规则{连接A--->资源A,连接B--->资源B....}
var myrouter = new VueRouter({
//设置路由匹配规则: routes
routes:[
{path:"/login",component:{template:"#logintp"}},
{path:"/zhuce",component:{template:"#zhucetp"}}
]
});
var vm = new Vue({
el:"#app",
router:myrouter //将路由对象挂载到Vue对象中
});
</script>
</body>
</html>
结果
点击login

点击zhuce

二级路由

示例
<html>
<head>
<meta charset="UTF-8">
<title>二级路由</title>
<!--导入Vue及路由-->
<script type="text/javascript" src="js/vue-2.4.0.js" ></script>
<script type="text/javascript" src="js/vue-router-3.0.1.js" ></script>
</head>
<body>
<div id="app">
<!--编写路由链接: 类似a标签,作为1个跳转热点内容-->
<router-link to="/login">login</router-link>
<router-link to="/zhuce">zhuce</router-link>
<!--创建路由出口: 专门用于展示路由资源的位置-->
<router-view></router-view>
</div>
<!--一级路由组件内容-->
<!--一级路由登录模板-->
<template id="logintp">
<div>
<!--编写二级路由链接: 类似a标签,作为1个跳转热点内容-->
<router-link to="/login/qq">qq</router-link><br />
<router-link to="/login/weixin">微信</router-link><br />
<!--创建二级路由出口: 专门用于展示路由资源的位置-->
<router-view></router-view>
</div>
</template>
<!--一级路由注册模板-->
<template id="zhucetp">
<div>
<!--编写二级路由链接: 类似a标签,作为1个跳转热点内容-->
<router-link to="/zhuce/shouji">手机</router-link><br />
<router-link to="/zhuce/carid">身份证</router-link><br />
<!--创建二级路由出口: 专门用于展示路由资源的位置-->
<router-view></router-view>
</div>
</template>
<!--二级路由模板-->
<template id="qqtp">
<span>qq登录</span>
</template>
<template id="weixintp">
<span>微信登录</span>
</template>
<template id="shoujitp">
<span>手机号注册</span>
</template>
<template id="caridtp">
<span>身份证号注册</span>
</template>
<script type="text/javascript">
//创建路由对象: 路由规则{连接A--->资源A,连接B--->资源B....}
var myrouter = new VueRouter({
//设置路由匹配规则: routes
routes:[
{path:"/login",component:{template:"#logintp"},
//定义二级路由匹配规则
children:[
{path:"qq",component:{template:"#qqtp"}},
{path:"weixin",component:{template:"#weixintp"}}
]
},
{path:"/zhuce",component:{template:"#zhucetp"},
children:[
{path:"shouji",component:{template:"#shoujitp"}},
{path:"carid",component:{template:"#caridtp"}}
]
}
]
});
var vm = new Vue({
el:"#app",
router:myrouter //将路由对象挂载到Vue对象中
});
</script>
</body>
</html>
结果
点击login在点击qq的话

点击login在点击微信的话

路由传参

示例
<html>
<head>
<meta charset="UTF-8">
<title>路由传参</title>
<!--导入Vue及路由-->
<script type="text/javascript" src="js/vue-2.4.0.js" ></script>
<script type="text/javascript" src="js/vue-router-3.0.1.js" ></script>
</head>
<body>
<div id="app">
<!--编写路由链接: 类似a标签,作为1个跳转热点内容-->
<router-link to="/login/:1/:张三">login</router-link>
<router-link to="/zhuce?id=2&name=李四">zhuce</router-link>
<!--创建路由出口: 专门用于展示路由资源的位置-->
<router-view></router-view>
</div>
<!--模板-->
<template id="logintp">
<!--使用插值表达式显示传来的数据-->
<h1>登录 参1{{$route.params.id}},参2{{$route.params.name}}</h1>
</template>
<template id="zhucetp">
<h1>注册 参1{{$route.query.id}},参2{{$route.query.name}}</h1>
</template>
<script type="text/javascript">
//创建路由对象: 路由规则{连接A--->资源A,连接B--->资源B....}
var myrouter = new VueRouter({
//设置路由匹配规则: routes
routes:[
{path:"/login/:id/:name",component:{template:"#logintp"}},//ROST方式传参
{path:"/zhuce",component:{template:"#zhucetp"}}//URL方式传参
]
});
var vm = new Vue({
el:"#app",
router:myrouter //将路由对象挂载到Vue对象中
});
</script>
</body>
</html>
结果
点击login

点击zhuce

编程式路由

示例
<html>
<head>
<meta charset="UTF-8">
<title>编程式路由</title>
<!--导入Vue及路由-->
<script type="text/javascript" src="js/vue-2.4.0.js" ></script>
<script type="text/javascript" src="js/vue-router-3.0.1.js" ></script>
</head>
<body>
<div id="app">
<!--编写路由链接: 类似a标签,作为1个跳转热点内容-->
<!--使用按钮替代router-link标签-->
<button @click="tologin">登录</button>
<button @click="tozhuce">注册</button>
<!--创建路由出口: 专门用于展示路由资源的位置-->
<router-view></router-view>
</div>
<!--模板-->
<template id="logintp">
<h1>登录</h1>
</template>
<template id="zhucetp">
<h1>注册</h1>
</template>
<script type="text/javascript">
//创建路由对象: 路由规则{连接A--->资源A,连接B--->资源B....}
var myrouter = new VueRouter({
//设置路由匹配规则: routes
routes:[
{path:"/login",component:{template:"#logintp"}},
{path:"/zhuce",component:{template:"#zhucetp"}}
]
});
var vm = new Vue({
el:"#app",
router:myrouter, //将路由对象挂载到Vue对象中
methods:{
tologin:function(){
//实现路由跳转的功能
this.$router.push({path:"/login"});
},
tozhuce:function(){
this.$router.push({path:"/zhuce"});
}
}
});
</script>
</body>
</html>
结果
点击登录

点击注册

编程式路由的其他方法

导航守卫
介绍

导航守卫解析流程

全局守卫
全局前置导航守卫

全局后置导航守卫

示例
<html>
<head>
<meta charset="UTF-8">
<title>全局导航守卫</title>
<!--导入Vue及路由-->
<script type="text/javascript" src="js/vue-2.4.0.js" ></script>
<script type="text/javascript" src="js/vue-router-3.0.1.js" ></script>
</head>
<body>
<div id="app">
<!--编写路由链接: 类似a标签,作为1个跳转热点内容-->
<router-link to="/login">login</router-link>
<router-link to="/zhuce">zhuce</router-link>
<!--创建路由出口: 专门用于展示路由资源的位置-->
<router-view></router-view>
</div>
<!--模板-->
<template id="logintp">
<h1>登录</h1>
</template>
<template id="zhucetp">
<h1>注册</h1>
</template>
<script type="text/javascript">
//创建路由对象: 路由规则{连接A--->资源A,连接B--->资源B....}
var myrouter = new VueRouter({
//设置路由匹配规则: routes
routes:[
{path:"/login",component:{template:"#logintp"}},
{path:"/zhuce",component:{template:"#zhucetp"}}
]
});
//全局前置导航守卫: 所有路由跳转之前,执行此方法
myrouter.beforeEach(function(to,from,next){
console.log("beforeEach来自:"+from.path);//来自哪里
console.log("beforeEach去哪:"+to.path);//去往哪里
next();//释放链接next({path:...})
});
//全局后置导航守卫: 所有路由跳转之后,执行此方法
myrouter.afterEach(function(to,from){
console.log("afterEach来自:"+from.path);//来自哪里
console.log("afterEach去哪:"+to.path);//去往哪里
});
var vm = new Vue({
el:"#app",
router:myrouter //将路由对象挂载到Vue对象中
});
</script>
</body>
</html>
结果
点击login

点击zhuce

如果把next()注释掉的话
//全局前置导航守卫: 所有路由跳转之前,执行此方法
myrouter.beforeEach(function(to,from,next){
console.log("beforeEach来自:"+from.path);//来自哪里
console.log("beforeEach去哪:"+to.path);//去往哪里
//next();//释放链接next({path:...})
});
结果
点击login不显示登录也不会执行后置守卫。

路由独享守卫

示例
<html>
<head>
<meta charset="UTF-8">
<title>路由独享守卫</title>
<!--导入Vue及路由-->
<script type="text/javascript" src="js/vue-2.4.0.js" ></script>
<script type="text/javascript" src="js/vue-router-3.0.1.js" ></script>
</head>
<body>
<div id="app">
<!--编写路由链接: 类似a标签,作为1个跳转热点内容-->
<router-link to="/login">login</router-link>
<router-link to="/zhuce">zhuce</router-link>
<!--创建路由出口: 专门用于展示路由资源的位置-->
<router-view></router-view>
</div>
<!--模板-->
<template id="logintp">
<h1>登录</h1>
</template>
<template id="zhucetp">
<h1>注册</h1>
</template>
<script type="text/javascript">
//创建路由对象: 路由规则{连接A--->资源A,连接B--->资源B....}
var myrouter = new VueRouter({
//设置路由匹配规则: routes
routes:[
{path:"/login",component:{template:"#logintp"},
//设置该路由独享守卫
beforeEnter:function(to,from,next){//进入当前路由资源之前执行
console.log("beforeEnter 来自:"+from.path);//来自哪里
console.log("beforeEnter 去哪:"+to.path);//去往哪里
next();//释放链接next({path:...})
}
},
{path:"/zhuce",component:{template:"#zhucetp"}}
]
});
var vm = new Vue({
el:"#app",
router:myrouter //将路由对象挂载到Vue对象中
});
</script>
</body>
</html>
结果
点击注册会发现没有触发该守卫

在点击login发现守卫已触发,因为这是在login路由内部设置的守卫。

组件内的守卫

示例
<html>
<head>
<meta charset="UTF-8">
<title>组件内导航守卫 </title>
<!--导入Vue及路由-->
<script type="text/javascript" src="js/vue-2.4.0.js" ></script>
<script type="text/javascript" src="js/vue-router-3.0.1.js" ></script>
</head>
<body>
<div id="app">
<!--编写路由链接: 类似a标签,作为1个跳转热点内容-->
<router-link to="/login">login</router-link>
<router-link to="/login2/1">login2</router-link>
<router-link to="/login2/2">login2</router-link>
<router-link to="/zhuce">zhuce</router-link>
<!--创建路由出口: 专门用于展示路由资源的位置-->
<router-view></router-view>
</div>
<!--模板-->
<template id="logintp">
<h1>登录</h1>
</template>
<template id="zhucetp">
<h1>注册</h1>
</template>
<script type="text/javascript">
var logincom={
template:"#logintp",
//在渲染的组件的对应路由之前被调用
beforeRouteEnter(to,from,next){
console.log("beforeRouteEnter 来自:"+from.path);
console.log("beforeRouteEnter 去哪:"+to.path);
next();//释放链接next({path:...})
},
//当前路由改变 ,但该组件被复用时调用
beforeRouteUpdate(to,from,next){
console.log("beforeRouteUpdate 来自:"+from.path);
console.log("beforeRouteUpdate 去哪:"+to.path);
next();//释放链接next({path:...})
},
//导航离开该组件的对应路由时被调用
beforeRouteLeave(to,from,next){
console.log("beforeRouteLeave 来自:"+from.path);
console.log("beforeRouteLeave 去哪:"+to.path);
next();//释放链接next({path:...})
}
}
//创建路由对象: 路由规则{连接A--->资源A,连接B--->资源B....}
var myrouter = new VueRouter({
//设置路由匹配规则: routes
routes:[
{path:"/login",component:logincom},
{path:"/login2/:id",component:logincom},
{path:"/zhuce",component:{template:"#zhucetp"}}
]
});
var vm = new Vue({
el:"#app",
router:myrouter //将路由对象挂载到Vue对象中
});
</script>
</body>
</html>
结果
点击login在点击第一个login2会发现beforeRouteUpdate没有触发

在点击第二个login2 会发现beforeRouteUpdate已经触发

在点击第一个login2会发现也会触发beforeRouteUpdate

本文详细介绍了Vue项目的路由应用,包括前端路由的概念、配置与使用,如一级和二级路由的设定,路由参数传递,以及编程式路由的方法。同时,深入探讨了导航守卫,包括全局守卫、路由独享守卫和组件内的守卫,通过实例展示了导航守卫的工作流程和应用场景。
1074

被折叠的 条评论
为什么被折叠?



