在做单页面应用的时候很方便,可以在界面中定义的各种区域进行组件替换,每个路由应该对应一个组件
<template>
<div>
<div>
<div>this is foorouterdiv>
div>
div>
template>
<template>
<div>
<div>this is barrouterdiv>
<p>username:{{username}}p>
<button @click="goBack">回到上一层button>
div>
template>
<script>
export default {
computed: {
username:function(){
// 我们很快就会看到 `params` 是什么
return 'Nelson'
}
},
methods: {
goBack() {
console.log("window.history:",window.history)
window.history.length > 1 ? this.$router.go(-1) : this.$router.push('/')
}
}
}
script>
const routes = [
{ path: '/foorouter', component: Foorouter },
{ path: '/barrouter', component: Barrouter }
]
const router = new VueRouter({
routes
})

说明
这里是默认的出口,还有命名视图,后面会介绍
代码
<p class="title">路由匹配到的组件将渲染在这里 ↓:p>
<router-view>router-view>
效果

说明
可以使用
代码
<router-link to="/foorouter">Go to Foorouter-link>
<router-link to="/barrouter">Go to Barrouter-link>
效果

在一.2步骤中,定义一个动态路由

User组件
<template>
<div>
<div>Userdiv>
Hello {{$route.params.id}}
<br>
{{message}}
div>
template>
<script>
export default {
watch:{
$route(to,from){
//对路由变化作出响应
console.log('From: '+from.path);
console.log('To: '+to.path);
}
},
computed:{
message:function(){
return '这是'+this.$route.params.id+'页面。'
}
}
}
script>
在页面定义路由导航
在两个导航中会分别把foorouter、barrouter作为参数匹配到.$route.params动态参数id中,在组件中可以使用this.$route.params调用 (如上面的User组件的massage计算属性中)
<router-link to="/user/foorouter">Go to UserFoorouter-link>
<router-link to="/user/barrouter">Go to UserBarrouter-link>
效果

说明
代码

说明
可以简单地 watch (监测变化) $route 对象
比如 1 中定义的User组件定义了响应方法,当外部页面点击两个链接,路径会变化,从而执行对应方法
代码

效果

说明
就是在路由的组件内部再定义路由出口,内部的路径需要在children属性中定义,注意不要加/ ,因为/代表根路径,定义内部路由的时候需要在外部路由的基础上定义相对匹配路径
代码

<template>
<div>
<h2>NestComponent: {{ $route.params.nestID }}h2>
<router-view>router-view>
div>
template>
<template>
<div>
<div>Nest-Userdiv>
Hello {{$route.params.nestID}}
<br>
嵌套内部的组件NestUser
div>
template>
<script>
export default {
}
script>
<template>
<div>
<div class="notice">Nest-UserAdmindiv>
Hello {{$route.params.nestID}}
<div class="notice">嵌套内部的组件NestUserAdmindiv>
div>
template>
<script>
export default {
watch:{
$route(to,from){
//对路由变化作出响应
console.log('From: '+from.path);
console.log('To: '+to.path);
}
}
}
script>
<style>
.notice{
color: red;
font-size: 30px;
}
style>
效果

注意
上面代码中内部组件监听$route变化是不起作用的,需要在外部组件监听。
说明
代码
<button v-on:click='addRouterItem'>编程式导航:push跳转路由信息button>
addRouterItem:function(){
const userid='TurnTo'
this.$router.push({path:`/Nest/${userid}/user`})
console.log('Success Turn To Router Item')
},
效果


还可以用 this.$router.push({ name: ‘user’, params: { userId: ‘123’ }}) 的形式来访问user/123页面,当有path的时候,params参数失效
说明
replace和push功能类似,但是注意它是直接替换window.history中这个当前的记录,而不是在window.history中添加,所以在使用this.$router.go()方法时 后退不会回到刚才的页面 。
效果

如图,在第三个页面的时候直接点击页面返回按钮,则回到的是第一次的页面

<button v-on:click='GoNamedRouterItem'>跳转命名路由信息button>
GoNamedRouterItem:function(){
this.$router.push({name:'useridrouter'})
}

未命名的默认名为default
{
path:'/multiview/',
components:{
viewone:ViewOneComp,
viewtwo:ViewTwoComp,
viewthree:ViewThreeComp
}
}
<div class="title">-----------------测试多视图-----------------div>
<button v-on:click='GoToMultiRouters'>跳转到命名(多)视图button>
<button v-on:click='HideMultiRouters'>隐藏命名(多)视图button>
<router-view name="viewone">router-view>
<router-view name="viewtwo">router-view>
<router-view name="viewthree">router-view>

<template>
<div>
<h1>显示嵌套命名(多)视图</h1>
<button v-on:click="ShowEmailView">显示Email视图</button>
<button v-on:click="ShowProfileView">显示Profile视图</button>
<router-view></router-view>
<router-view name="helper"></router-view>
</div>
</template>
<script>
export default {
methods:{
ShowEmailView:function(){
this.$router.push('/settings/emails');
},
ShowProfileView:function(){
this.$router.push('/settings/profile');
}
}
}
</script>
{
path:'/settings/',
component:UserSettings,
children:[{
path:'emails',
component:UserSettingEmail
},{
path:'profile',
components:{
default:UserSettingProfile,
helper:UserSettingProfilePreview
}
}]
},

GoToRedirectFoo:function(){
this.$router.push('/tempTofoorouter')
},
{ path:'/tempTofoorouter',redirect:'/foorouter'},

代码
在routes路由设置中redirect传入一个函数,返回重定向路径,其中to是包含目的路径的对象
{ path:'/user',redirect:to=>{
console.log("to:",to)
return to.path+'/nel'
}
},
函数中可以定义怎么对原目的路径进行处理,返回新的路径。
GoToRedirectBar:function(){
this.$router.push('/user')
},
效果

{ path:'/foorouter',component:Foorouter,alias:'/aliasfoo'}
说明
使用 props 将组件和路由解耦可以向路由组件传递prop
代码
{ path: '/user/:id/:message', component: User,props:true}
<template>
<div>
<div>Userdiv>
Hello {{id}}
<br>
{{message}}
div>
template>
<script>
export default {
props:['id','message'],
}
script>
<router-link to="/user/foorouter/这是通过props传递param的页面">Go to UserFoorouter-link>
效果
