Vue Router(三):命名视图、重定向和组件传参

命名视图

同时 (同级) 展示多个视图,而不是嵌套展示

如果 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>
routes: [
    {
      path: '/',
      components: {
        default: Foo,
        a: Bar,
        b: Baz
      }
    }
  ]

重定向

{ path: '/a', redirect: '/b' }
// 或对象形式
{ path: '/a', redirect: { name: 'foo' }}
// 甚至是一个方法,动态返回重定向目标
{ path: '/a', redirect: to => {
	// 方法接收 目标路由 作为参数
	// return 重定向的 字符串路径/路径对象
}}

注意:导航守卫并没有应用在跳转路由上,而仅仅应用在其目标上。

别名

/a 的别名是 /b,意味着,当用户访问 /b 时,URL 会保持为 /b,但是路由匹配则为 /a,就像用户访问 /a 一样。

{ path: '/a', component: A, alias: '/b' }

“别名”的功能让你可以自由地将 UI 结构映射到任意的 URL,而不是受限于配置的嵌套路由结构。

路由组件传参

使用 props 将组件和路由解耦:

const User = {
  props: ['id'],
  template: '<div>User {{ id }}</div>'
}
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 }
    }
  ]
})
  • 如果 props 被设置为 true,route.params 将会被设置为组件属性
  • 如果 props 是一个对象,它会被按原样设置为组件属性。当 props 是静态的时候有用。
  • 可以创建一个函数返回 props。这样你便可以将参数转换成另一种类型,将静态值与基于路由的值结合
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值