目录
1.多视图路由
1.1命名视图
1.2嵌套命名视图
2.重定向和别名
2.1重定向
2.2别名
3.路由组件传参
3.1问号传参
3.2路径传参
3.3编程式导航传参
4.HTML5History模式
1.多视图路由 1.1命名视图有时候想同时 (同级) 展示多个视图,而不是嵌套展示,例如创建一个布局,有 sidebar
(侧导航) 和 main
(主内容) 两个视图,这个时候命名视图就派上用场了。你可以在界面中拥有多个单独命名的视图,而不是只有一个单独的出口。如果 router-view
没有设置名字,那么默认为 default
。
一个视图使用一个组件渲染,因此对于同个路由,多个视图就需要多个组件。确保正确使用 components
配置 (带上 s):
const router = new VueRouter({
routes: [
{
path: '/',
components: {
default: Foo,
a: Bar,
b: Baz
}
}
]
})
1.2嵌套命名视图
我们也有可能使用命名视图创建嵌套视图的复杂布局。这时你也需要命名用到的嵌套 router-view
组件。我们以一个设置面板为例:
/settings/emails /settings/profile
+-----------------------------------+ +------------------------------+
| UserSettings | | UserSettings |
| +-----+-------------------------+ | | +-----+--------------------+ |
| | Nav | UserEmailsSubscriptions | | +------------> | | Nav | UserProfile | |
| | +-------------------------+ | | | +--------------------+ |
| | | | | | | | UserProfilePreview | |
| +-----+-------------------------+ | | +-----+--------------------+ |
+-----------------------------------+ +------------------------------+
Nav
只是一个常规组件。UserSettings
是一个视图组件。UserEmailsSubscriptions
、UserProfile
、UserProfilePreview
是嵌套的视图组件。
注意:我们先忘记 HTML/CSS 具体的布局的样子,只专注在用到的组件上。
UserSettings
组件的 部分应该是类似下面的这段代码:
User Settings
然后你可以用这个路由配置完成该布局:
{
path: '/settings',
// 你也可以在顶级路由就配置命名视图
component: UserSettings,
children: [{
path: 'emails',
component: UserEmailsSubscriptions
}, {
path: 'profile',
components: {
default: UserProfile,
helper: UserProfilePreview
}
}]
}
2.重定向和别名
2.1重定向
重定向也是通过 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 重定向的 字符串路径/路径对象
}}
]
})
2.2别名
“重定向”的意思是,当用户访问 /a
时,URL 将会被替换成 /b
,然后匹配路由为 /b
,那么“别名”又是什么呢?
/a
的别名是 /b
,意味着,当用户访问 /b
时,URL 会保持为 /b
,但是路由匹配则为 /a
,就像用户访问 /a
一样。
上面对应的路由配置为:
const router = new VueRouter({
routes: [
{ path: '/a', component: A, alias: '/b' }
]
})
“别名”的功能让你可以自由地将 UI 结构映射到任意的 URL,而不是受限于配置的嵌套路由结构。
3.路由组件传参在组件中使用 $route
会使之与其对应路由形成高度耦合,从而使组件只能在某些特定的 URL 上使用,限制了其灵活性。
使用 props
将组件和路由解耦:
取代与 $route
的耦合
const User = {
template: 'User {{ $route.params.id }}'
}
const router = new VueRouter({
routes: [{ path: '/user/:id', component: User }]
})
通过 props
解耦
const User = {
props: ['id'],
template: 'User {{ id }}'
}
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User, props: true },
// 对于包含命名视图的路由,你必须分别为每个命名视图添加 `props` 选项:
{
path: '/user/:id',
components: { default: User, sidebar: Sidebar },
props: { default: true, sidebar: false }
}
]
})
这样你便可以在任何地方使用该组件,使得该组件更易于重用和测试。
1)布尔模式
如果 props
被设置为 true
,route.params
将会被设置为组件属性。
2)对象模式
如果 props
是一个对象,它会被按原样设置为组件属性。当 props
是静态的时候有用。
const router = new VueRouter({
routes: [
{
path: '/promotion/from-newsletter',
component: Promotion,
props: { newsletterPopup: false }
}
]
})
3)函数模式
你可以创建一个函数返回 props
。这样你便可以将参数转换成另一种类型,将静态值与基于路由的值结合等等。
const router = new VueRouter({
routes: [
{
path: '/search',
component: SearchUser,
props: route => ({ query: route.query.q })
}
]
})
URL /search?q=vue
会将 {query: 'vue'}
作为属性传递给 SearchUser
组件。
请尽可能保持 props
函数为无状态的,因为它只会在路由发生变化时起作用。如果你需要状态来定义 props
,请使用包装组件,这样 Vue 才可以对状态变化做出反应。
我们可以在路由中,使用查询字符串的方式给路由传递参数。
商品列表页面
商品名:{{goods.name}}
价格:{{goods.price}}
当然上面传递的方式就类似于传统的问号传参方式。比如 /detail?gid=4
这样的方式传递,如果想写在to里面就用上面的方式。
给路由传递参数后,我们并不需要修改路由匹配规则。
我们给路由传递参数就是为了拿到参数,那么如何获取参数呢?
在我们的组件模板对象里面,可以使用this.$route.query
的方式获取我们传入的参数对象。
export let Detail = {
template : require("./index.html"),
data(){
return {
goodsid : 0
}
},
// 加载后获取路由参数
mounted(){
let goodsid = this.$route.query.gid
this.goodsid = goodsid
}
}
3.2路径传参
首先在路由匹配规则中,使用占位符代替路由传递时的参数。
path 中的 :newGid
表示如果以后路由后面跟内容了,那么内容的属性就是gid。
{path : '/detail/:newGid',component:Detail,name:"d"},
然后在传递参数时,就不是用query了,而是params。
商品列表页面
商品名:{{goods.name}}
价格:{{goods.price}}
这时候,传过去时就不是问号的形式,而是路径的形式,如:/detail/4
获取的时候也不是query而是params,this.$route.params
。
export let Detail = {
template : require("./index.html"),
data(){
return {
goodsid : 0
}
},
// 加载后获取路由参数
mounted(){
let newGoodsid = this.$route.params.newGid;
this.goodsid = newGoodsid;
}
}
3.3编程式导航传参
上面的路由传参都是采用to传参,也就是标签传参,怎么使用编程式导航传参?
其实和to是一样的:
this.$router.push({name:'d',query:{gid:goods.goodsID}})
this.$router.push({name:'d',params:{newGid:goods.goodsID}})
4.HTML5History模式
vue-router
默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载。
如果不想要很丑的 hash,我们可以用路由的 history 模式,这种模式充分利用 history.pushState
API 来完成 URL 跳转而无须重新加载页面。
const router = new VueRouter({
mode: 'history',
routes: [...]
})
当你使用 history 模式时,URL 就像正常的 url,例如 http://yoursite.com/user/id
,也好看!
不过这种模式要玩好,还需要后台配置支持。因为我们的应用是个单页客户端应用,如果后台没有正确的配置,当用户在浏览器直接访问 http://oursite.com/user/id
就会返回 404,这就不好看了。
所以呢,你要在服务端增加一个覆盖所有情况的候选资源:如果 URL 匹配不到任何静态资源,则应该返回同一个 index.html
页面,这个页面就是你 app 依赖的页面。