Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #6263: $attrs is undefined when component has no props bug (#6263) #6441

Merged
merged 3 commits into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions flow/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ declare interface Component {
$slots: { [key: string]: Array<VNode> };
$scopedSlots: { [key: string]: () => VNodeChildren };
$vnode: VNode; // the placeholder node for the component in parent's render tree
$attrs: ?{ [key: string] : string };
$listeners: ?{ [key: string]: Function | Array<Function> };
$attrs: { [key: string] : string };
$listeners: { [key: string]: Function | Array<Function> };
$isServer: boolean;

// public methods
Expand Down
4 changes: 2 additions & 2 deletions src/core/instance/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ export function updateChildComponent (
// update $attrs and $listensers hash
// these are also reactive so they may trigger child update if the child
// used them during render
vm.$attrs = parentVnode.data && parentVnode.data.attrs
vm.$listeners = listeners
vm.$attrs = (parentVnode.data && parentVnode.data.attrs) || emptyObject
vm.$listeners = listeners || emptyObject

// update props
if (propsData && vm.$options.props) {
Expand Down
9 changes: 5 additions & 4 deletions src/core/instance/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,18 @@ export function initRender (vm: Component) {
// $attrs & $listeners are exposed for easier HOC creation.
// they need to be reactive so that HOCs using them are always updated
const parentData = parentVnode && parentVnode.data

/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production') {
defineReactive(vm, '$attrs', parentData && parentData.attrs, () => {
defineReactive(vm, '$attrs', parentData && parentData.attrs || emptyObject, () => {
!isUpdatingChildComponent && warn(`$attrs is readonly.`, vm)
}, true)
defineReactive(vm, '$listeners', vm.$options._parentListeners, () => {
defineReactive(vm, '$listeners', vm.$options._parentListeners || emptyObject, () => {
!isUpdatingChildComponent && warn(`$listeners is readonly.`, vm)
}, true)
} else {
defineReactive(vm, '$attrs', parentData && parentData.attrs, null, true)
defineReactive(vm, '$listeners', vm.$options._parentListeners, null, true)
defineReactive(vm, '$attrs', parentData && parentData.attrs || emptyObject, null, true)
defineReactive(vm, '$listeners', vm.$options._parentListeners || emptyObject, null, true)
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit/features/instance/properties.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ describe('Instance properties', () => {
}).then(done)
})

// #6263
it('$attrs should not be undefined when no props passed in', () => {
const vm = new Vue({
template: `<foo/>`,
data: { foo: 'foo' },
components: {
foo: {
template: `<div>{{ this.foo }}</div>`
}
}
}).$mount()
expect(vm.$attrs).toBeDefined()
})

it('warn mutating $attrs', () => {
const vm = new Vue()
vm.$attrs = {}
Expand Down
4 changes: 2 additions & 2 deletions types/vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export declare class Vue {
readonly $ssrContext: any;
readonly $props: any;
readonly $vnode: VNode;
readonly $attrs: { [key: string] : string } | void;
readonly $listeners: { [key: string]: Function | Array<Function> } | void;
readonly $attrs: { [key: string] : string };
readonly $listeners: { [key: string]: Function | Array<Function> };

$mount(elementOrSelector?: Element | String, hydrating?: boolean): this;
$forceUpdate(): void;
Expand Down