什么是路由
- 后端路由:对于普通的网站,所有的超链接都是URL地址,所有的URL地址都对应服务器上对应的资源
- 前端路由:对于单页面应用程序来说,主要通过URL中的hash(#号)来实现不同页面之间的切换,同时,hash有一个特点:HTTP请求中不会包含hash相关的内容;所以,单页面程序中的页面跳转主要用hash实现;
- 在单页面应用程序中,这种通过hash改变来切换页面的方式,称作前端路由(区别于后端路由)
如何使用路由
路由的安装
直接引用官网提供的cdn
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
路由的基本使用
-
引入js文件,这个js需要放在vue的js后面,自动安装(提供了一个VueRouter的构造方法)
-
创建路由new VueRouter(),接受的参数是一个对象
-
在实例化的对象里配置属性routes:[],这个数组里的对象包含path属性和component属性
-
path属性是url的地址,component属性就是显示的组件(传组件的对象)
-
创建的路由需要和vue实例关联一下
-
路由到的组件显示在哪个位置
实例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!--1. 引入vuerouter -->
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id='app'>
<!-- 5、预留展示区域 -->
<router-view></router-view>
</div>
<template id="son">
<div>
子组件
</div>
</template>
<script>
let son = Vue.component("son", {
template: "#son"
})
// 2、创建路由实例
const router = new VueRouter({
// 3、创建组件和路由映射关系
routes: [
{
path: "/",
component: Vue.component("haha", {
template: "<h2>hahah</h2>"
})
},
{
path: "/son",
component: son
}]
});
const vm = new Vue({
el: '#app',
data: {
},
methods: {
},
//4. 创建联系,挂载在Vue实例上
router
})
</script>
</body>
</html>
路由的跳转
-
router-link标签可以设置to属性
-
默认是a标签,可以通过tag设置包裹标签
//路由的跳转
<router-link to='/login'>登录</router-link>
// 通过tag可以指定router-link渲染的界面元素 转换成div标签
<router-link to='/registry' tag="div">注册</router-link>
路由重定向
redirect可以进行路由的重定向
实例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!--1. 引入vuerouter -->
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id='app'>
<router-view></router-view>
</div>
<!-- 子组件 -->
<template id="son">
<div>
子组件
</div>
</template>
<!-- 父组件 -->
<template id="father">
<div>
父组件
<router-link to="/son">去子组件</router-link>
<router-link to="/son" tag="div">去子组件tag改变标签</router-link>
</div>
</template>
<script>
let son = Vue.component("son", {
template: "#son"
})
let father = {
template: "#father"
}
const router = new VueRouter({
routes: [
{
path: "/index",
component: father,
},
{
//路由重定向
//通过这种方式,在访问/路径的时候会重定向到/index路径
path: "/",
redirect: "/index"
},
{
path: "/son",
component: son
}]
});
const vm = new Vue({
el: '#app',
data: {
},
methods: {
},
//ES6方法 把路由挂在到实例上
router,
})
</script>
</body>
</html>
选中路由高亮
- 使用默认的样式
- 直接设置router-link-active
.router-link-active {
color: red;
font-size: 40px;
}
- 自定义样式
- 配置 linkActiveClass:'自定义的类名'
.yanse {
color: pink;
font-size: 50px;
font-weight: 700;
}
const router = new VueRouter({
linkActiveClass: "yanse"
})
定义参数
通过query的方式在url后加?参数名=参数的值
获取参数:$route.query.参数名
实例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!--1. 引入vuerouter -->
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<style>
.router-link-active {
color: red;
font-size: 40px;
}
.yanse {
color: pink;
font-size: 50px;
font-weight: 700;
}
</style>
<body>
<div id='app'>
<router-view></router-view>
</div>
<!-- 子组件 -->
<template id="son">
<div>
子组件
<router-link to="/son">去子组件吧</router-link>
<router-link to="/index?courseid=103">去父组件吧</router-link>
<!-- 路由传参 -->
<router-link :to="{path:'/index',query:{course:112}}">去父组件2</router-link>
<router-link :to="{path:'/index',query:{course:courseid}}">去父组件3</router-link>
</div>
</template>
<!-- 父组件 -->
<template id="father">
<div>
父组件
<router-link to="/son">去子组件吧</router-link>
<router-link to="/index">去父组件吧</router-link>
<router-link to="/son" tag="div"> 去子组件tag改变标签</router-link>
</div>
</template>
<script>
let son = Vue.component("son", {
template: "#son",
data() {
return {
courseid: 223,
};
},
})
let father = {
template: "#father",
data() {
return {
num: 223,
}
},
created() {
console.log(this.$route);
console.log(this.$route.query);
}
}
const router = new VueRouter({
routes: [
{
path: "/index",
component: father,
},
{
path: "/",
redirect: "/index"
// 定义必传参数
},
{
path: "/son/:id/:name",
component: son,
},
],
linkActiveClass: "yanse"
});
const vm = new Vue({
el: '#app',
data: {
},
methods: {
},
router,
})
</script>
</body>
</html>
使用浏览器参数的方式传递参数
-
设置路由的时候/路由地址/:参数名
-
获取参数$route.params.参数名
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!--1. 引入vuerouter -->
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<style>
.router-link-active {
color: red;
font-size: 40px;
}
.yanse {
color: pink;
font-size: 50px;
font-weight: 700;
}
</style>
<body>
<div id='app'>
<router-view></router-view>
</div>
<!-- 子组件 -->
<template id="son">
<div>
子组件
<router-link to="/son">去子组件吧</router-link>
<router-link to="/index?courseid=103">去父组件吧</router-link>
<!-- 路由传参 -->
<!-- <router-link :to="{path:'/index',query:{course:112}}">去父组件2</router-link>
<router-link :to="{path:'/index',query:{course:courseid}}">去父组件3</router-link> -->
</div>
</template>
<!-- 父组件 -->
<template id="father">
<div>
父组件
<!-- <router-link to="/son">去子组件吧</router-link>
<router-link to="/index">去父组件吧</router-link>
<router-link to="/son" tag="div"> 去子组件tag改变标签</router-link> -->
<router-link to="/son/112">去子页面</router-link>
<router-link :to="{name:'son',params:{id:666}}">去子页面2</router-link>
<router-link :to="{name:'son',params:{id:num,name:'lili'}}">去子页面3</router-link>
</div>
</template>
<script>
let son = Vue.component("son", {
template: "#son",
data() {
return {
courseid: 223,
};
},
created() {
console.log(this.$route);
console.log(this.$route.params);
}
})
let father = {
template: "#father",
data() {
return {
num: 223,
}
},
created() {
console.log(this.$route);
console.log(this.$route.query);
}
}
const router = new VueRouter({
routes: [
{
path: "/index",
component: father,
},
{
path: "/",
redirect: "/index"
// 定义必传参数
},
{
path: "/son/:id/:name",
component: son,
name: "son",
},
],
linkActiveClass: "yanse"
});
const vm = new Vue({
el: '#app',
data: {
},
methods: {
},
router,
})
</script>
</body>
</html>