一 配置简单路由
- 首先 安装 npm install vue-router --save
- 在main.js, 引入路由和子组件,并配置路由
- App.vue 引用路由
main.js
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import TestOne from "./components/TestOne";
import TestTwo from "./components/TestTwo";
Vue.config.productionTip = false;
Vue.use(VueRouter);
const router = new VueRouter({
routes: [
{path:"/", component:TestOne},
{path:"/jump", component:TestTwo}
// 注意不是components
],
mode: "history"
});
new Vue({
render: h => h(App),
router
}).$mount('#app');
App.vue
<template>
<div id="app">
<p>llll</p>
<router-link to="/">点我跳转组件1</router-link>
<br>
<router-link to="/jump">点我跳转组件2</router-link>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'app',
components: {
}
}
</script>
二 动态路由
拿到url中匹配的参数
this.$route.params.id 注意是$route
{path:"/xx/:id, ..."} 路由这么配
{path:"/xx, ..."} 路由正常配
<router-link :to="'/xxx?aid'+ key ">
this.route.query.aid
模式 | 匹配路径 | $route.params |
---|---|---|
/user/:username | /user/evan | { username: 'evan' } |
/user/:username/post/:post_id | /user/evan/post/123 | { username: 'evan', post_id: '123' } |
path: "*" // 会匹配到剩下的虽有路径, 所以可以把它设置成最后一个路径来匹配,剩下的路径, 例如404
path: '/user-*' // 会匹配以 `/user-` 开头的任意路径
当使用一个通配符时,$route.params
内会自动添加一个名为 pathMatch
参数。它包含了 URL 通过通配符被匹配的部分:
// 给出一个路由 { path: '/user-*' }
this.$router.push('/user-admin')
this.$route.params.pathMatch // 'admin'
// 给出一个路由 { path: '*' }
this.$router.push('/non-existing')
this.$route.params.pathMatch // '/non-existing'
三 嵌套路由
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User,
children: [
{
// 当 /user/:id/profile 匹配成功,
// UserProfile 会被渲染在 User 的 <router-view> 中
path: 'profile',
component: UserProfile
},
{
// 当 /user/:id/posts 匹配成功
// UserPosts 会被渲染在 User 的 <router-view> 中
path: 'posts',
component: UserPosts
},
{ path: '', component: UserHome },
// 一个空的路由, 匹配其他没有指定的路由
]
}
]
})
四 编程式导航
当你点击 <router-link>
时,这个方法会在内部调用,所以说,点击 <router-link :to="...">
等同于调用 router.push(...)
。
声明式 | 编程式 |
---|---|
<router-link :to="..."> | router.push(...) |
跳转并添加路由参数
this.$router.push({path:"/", query:{msg:"信息添加成功!"}})
// 跳转
// 字符串
router.push('home')
// 对象
router.push({ path: 'home' })
// 命名的路由
router.push({ name: 'user', params: { userId: '123' }})
// 带查询参数,变成 /register?plan=private
router.push({ path: 'register', query: { plan: 'private' }})
router.replace(location, onComplete?, onAbort?)
跟 router.push
很像,唯一的不同就是,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。
声明式 | 编程式 |
---|---|
<router-link :to="..." replace> | router.replace(...) |
router.go(n)
这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n)
。
例子
// 在浏览器记录中前进一步,等同于 history.forward()
router.go(1)
// 后退一步记录,等同于 history.back()
router.go(-1)
// 前进 3 步记录
router.go(3)
// 如果 history 记录不够用,那就默默地失败呗
router.go(-100)
router.go(100)
五 命名路由
有时候,通过一个名称来标识一个路由显得更方便一些,特别是在链接一个路由,或者是执行一些跳转的时候。你可以在创建 Router 实例的时候,在 routes
配置中给某个路由设置名称。
const router = new VueRouter({
routes: [
{
path: '/user/:userId',
name: 'user',
component: User
}
]
})
要链接到一个命名路由,可以给 router-link
的 to
属性传一个对象:
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>
这跟代码调用 router.push()
是一回事:
router.push({ name: 'user', params: { userId: 123 }})
这两种方式都会把路由导航到 /user/123
路径。
六 命名视图
这个就是说一个路由渲染多个组件 components:
有时候想同时 (同级) 展示多个视图,而不是嵌套展示,例如创建一个布局,有 sidebar
(侧导航) 和 main
(主内容) 两个视图,这个时候命名视图就派上用场了。你可以在界面中拥有多个单独命名的视图,而不是只有一个单独的出口。如果 router-view
没有设置名字,那么默认为 default
。
<router-view class="view one"></router-view>
<router-view class="view two" name="a"></router-view>
<router-view class="view three" name="b"></router-view>
一个视图使用一个组件渲染,因此对于同个路由,多个视图就需要多个组件。确保正确使用 components
配置 (带上 s):
const router = new VueRouter({
routes: [
{
path: '/',
components: {
default: Foo,
a: Bar,
b: Baz
}
}
]
})
七 路由重定向
重定向
重定向也是通过 routes
配置来完成,下面例子是从 /a
重定向到 /b
:
const router = new VueRouter({
routes: [
{ path: '/a', redirect: '/b' }
]
})
重定向的目标也可以是一个命名的路由:
const router = new VueRouter({
routes: [
{ path: '/a', redirect: { name: 'foo' }}
]
})
甚至是一个方法,动态返回重定向目标:
const router = new VueRouter({
routes: [
{ path: '/a', redirect: to => {
// 方法接收 目标路由 作为参数
// return 重定向的 字符串路径/路径对象
}}
]
})
八 别名
const router = new VueRouter({
routes: [
{ path: '/a', component: A, alias: '/b' }
]
})
<router-link to="/b">下一步</router-link>
这个别名是我理解的别名,
和vue name 是不同的,
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>