路由组件传参

通过传参将组件与路由解耦,使得组件的使用更加灵活

在组件中使用 $route 会使之与其对应路由形成高度耦合,从而使组件只能在某些特定的 URL 上使用,限制了其灵活性。未使用 props 时:

const User = {
  template: '<div>User {{ $route.params.id }}</div>'
}
const router = new VueRouter({
  routes: [
    { path: '/user/:id', component: User }
  ]
})

通过 props 将组件与 $route 解耦的方法有以下几种:

布尔模式

在路由配置中设置 props: true 时默认将 $route.params 数据传给组件,组件需要通过自身的 props 属性取出 params 中的属性:

const User = { 
  props: ['userId', 'postId'], // 类似于 ES6 的对象解构,将 params 中的属性取出来
  template: '<div>{{ userId }} {{ postId }}</div>',
};

const router = new VueRouter({
  routes: [
    { 
      props: true, // 默认将 $route.params 传进去
      path: '/user/:userId/post/:postId',
      component: User,
    },
  ],
};

对象模式

如果 props 是一个对象,其下所有属性均会被传入组件。需要注意的是当 props 必须是是静态的:

函数模式

props 也可以是一个函数的返回值,这样的就可以将动态的 parmas 与静态值结合起来。上述的布尔形式的 props 也可以改写为如下:

除此之外,还可以动态 params 与静态数据相结合:

Last updated