Skip to content

Commit 2689a64

Browse files
author
hzzhaoxinhui
committed
fix is undefined when component has no props bug (vuejs#6263)
1 parent 82f03de commit 2689a64

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

flow/component.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ declare interface Component {
2929
$slots: { [key: string]: Array<VNode> };
3030
$scopedSlots: { [key: string]: () => VNodeChildren };
3131
$vnode: VNode; // the placeholder node for the component in parent's render tree
32-
$attrs: ?{ [key: string] : string };
32+
$attrs: { [key: string] : string };
3333
$listeners: ?{ [key: string]: Function | Array<Function> };
3434
$isServer: boolean;
3535

src/core/instance/render.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@ export function initRender (vm: Component) {
4949
// $attrs & $listeners are exposed for easier HOC creation.
5050
// they need to be reactive so that HOCs using them are always updated
5151
const parentData = parentVnode && parentVnode.data
52+
5253
/* istanbul ignore else */
5354
if (process.env.NODE_ENV !== 'production') {
54-
defineReactive(vm, '$attrs', parentData && parentData.attrs, () => {
55+
defineReactive(vm, '$attrs', parentData && parentData.attrs || {}, () => {
5556
!isUpdatingChildComponent && warn(`$attrs is readonly.`, vm)
5657
}, true)
5758
defineReactive(vm, '$listeners', vm.$options._parentListeners, () => {
5859
!isUpdatingChildComponent && warn(`$listeners is readonly.`, vm)
5960
}, true)
6061
} else {
61-
defineReactive(vm, '$attrs', parentData && parentData.attrs, null, true)
62+
defineReactive(vm, '$attrs', parentData && parentData.attrs || {}, null, true)
6263
defineReactive(vm, '$listeners', vm.$options._parentListeners, null, true)
6364
}
6465
}

test/unit/features/instance/properties.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ describe('Instance properties', () => {
146146
}).then(done)
147147
})
148148

149+
// #6263
150+
it('$attrs should not be undefined when no props passed in', () => {
151+
const vm = new Vue({
152+
template: `<foo/>`,
153+
data: { foo: 'foo' },
154+
components: {
155+
foo: {
156+
template: `<div>{{ this.foo }}</div>`
157+
}
158+
}
159+
}).$mount()
160+
expect(vm.$attrs).toBeDefined()
161+
})
162+
149163
it('warn mutating $attrs', () => {
150164
const vm = new Vue()
151165
vm.$attrs = {}

0 commit comments

Comments
 (0)