You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/essentials/passing-props.md
+24-12
Original file line number
Diff line number
Diff line change
@@ -2,27 +2,24 @@
2
2
3
3
<divclass="vueschool"><ahref="https://vueschool.io/lessons/how-to-pass-vue-router-params-as-props-to-components?friend=vuejs"target="_blank"rel="sponsored noopener"title="Learn how to pass props to route components with Vue School">Learn how to pass props to route components with a free lesson on Vue School</a></div>
4
4
5
-
6
5
Using `$route` in your component creates a tight coupling with the route which limits the flexibility of the component as it can only be used on certain URLs.
7
6
8
7
To decouple this component from the router use option `props`:
9
8
10
9
**Instead of coupling to `$route`:**
11
10
12
-
```js
11
+
```js
13
12
constUser= {
14
13
template:'<div>User {{ $route.params.id }}</div>'
15
14
}
16
15
constrouter=newVueRouter({
17
-
routes: [
18
-
{ path:'/user/:id', component: User }
19
-
]
16
+
routes: [{ path:'/user/:id', component: User }]
20
17
})
21
18
```
22
19
23
20
**Decouple it by using `props`**
24
21
25
-
```js
22
+
```js
26
23
constUser= {
27
24
props: ['id'],
28
25
template:'<div>User {{ id }}</div>'
@@ -34,8 +31,15 @@ const router = new VueRouter({
34
31
// for routes with named views, you have to define the `props` option for each named view:
35
32
{
36
33
path:'/user/:id',
37
-
components: { default: User, sidebar: Sidebar },
38
-
props: { default:true, sidebar:false }
34
+
components: {
35
+
default: User,
36
+
sidebar: Sidebar
37
+
},
38
+
props: {
39
+
default:true,
40
+
// function mode, more about it below
41
+
sidebar:route=> ({ search:route.query.q })
42
+
}
39
43
}
40
44
]
41
45
})
@@ -51,10 +55,14 @@ When `props` is set to `true`, the `route.params` will be set as the component p
51
55
52
56
When `props` is an object, this will be set as the component props as-is. Useful for when the props are static.
0 commit comments