Skip to content

Commit a722b6a

Browse files
lbogdanyyx990803
authored andcommitted
fix: send props not defined on the route component in $attrs. Fixes #1695. (#1702)
* Added repro test for #1695. * Send props not defined on the route component in $attrs. Fixes #1695.
1 parent 3b16bf1 commit a722b6a

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

examples/route-props/app.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,27 @@ function dynamicPropsFn (route) {
1111
}
1212
}
1313

14+
const Child = {
15+
name: 'child',
16+
props: { name: { default: 'name' }},
17+
template: `<div><h2 class="hello">Hello {{ name }}</h2></div>`
18+
}
19+
20+
const Parent = {
21+
name: 'parent',
22+
components: { Child },
23+
template: `<child v-bind="$attrs"></child>`
24+
}
25+
1426
const router = new VueRouter({
1527
mode: 'history',
1628
base: __dirname,
1729
routes: [
1830
{ path: '/', component: Hello }, // No props, no nothing
1931
{ path: '/hello/:name', component: Hello, props: true }, // Pass route.params to props
2032
{ path: '/static', component: Hello, props: { name: 'world' }}, // static values
21-
{ path: '/dynamic/:years', component: Hello, props: dynamicPropsFn } // custom logic for mapping between route and props
33+
{ path: '/dynamic/:years', component: Hello, props: dynamicPropsFn }, // custom logic for mapping between route and props
34+
{ path: '/attrs', component: Parent, props: { name: 'attrs' }}
2235
]
2336
})
2437

@@ -32,6 +45,7 @@ new Vue({
3245
<li><router-link to="/hello/you">/hello/you</router-link></li>
3346
<li><router-link to="/static">/static</router-link></li>
3447
<li><router-link to="/dynamic/1">/dynamic/1</router-link></li>
48+
<li><router-link to="/attrs">/attrs</router-link></li>
3549
</ul>
3650
<router-view class="view"></router-view>
3751
</div>

src/components/view.js

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ export default {
6969

7070
// resolve props
7171
data.props = resolveProps(route, matched.props && matched.props[name])
72+
data.attrs = {}
73+
74+
for (const key in data.props) {
75+
if (!('props' in component) || !(key in component.props)) {
76+
data.attrs[key] = data.props[key]
77+
delete data.props[key]
78+
}
79+
}
7280

7381
return h(component, data, children)
7482
}

test/e2e/specs/route-props.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
browser
44
.url('http://localhost:8080/route-props/')
55
.waitForElementVisible('#app', 1000)
6-
.assert.count('li a', 4)
6+
.assert.count('li a', 5)
77

88
.assert.urlEquals('http://localhost:8080/route-props/')
99
.assert.containsText('.hello', 'Hello Vue!')
@@ -20,6 +20,10 @@ module.exports = {
2020
.assert.urlEquals('http://localhost:8080/route-props/dynamic/1')
2121
.assert.containsText('.hello', 'Hello ' + ((new Date()).getFullYear() + 1)+ '!')
2222

23+
.click('li:nth-child(5) a')
24+
.assert.urlEquals('http://localhost:8080/route-props/attrs')
25+
.assert.containsText('.hello', 'Hello attrs')
26+
2327
.end()
2428
}
2529
}

0 commit comments

Comments
 (0)