Skip to content

chore #7070: warn properties that starts with $ or _ #7089

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,14 @@ function initData (vm: Component) {
`Use prop default value instead.`,
vm
)
} else if (!isReserved(key)) {
} else if (isReserved(key)) {
warn(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warn must be shown only in development mode (process.env.NODE_ENV !== production)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

`Property "${key}" will not be proxied on the instance because it ` +
`may conflict with Vue's internal properties. You will have to access it as ` +
`"vm.$data.${key}". Avoid defining component properties that start with _ or $.`,
vm
)
} else {
proxy(vm, `_data`, key)
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/unit/features/instance/properties.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,13 @@ describe('Instance properties', () => {
vm.$listeners = {}
expect(`$listeners is readonly`).toHaveBeenWarned()
})

it('warn data properties that start with _ or $', () => {
new Vue({
data: { $a: 'a', _b: 'b' },
template: '<div></div>'
}).$mount()
expect(`Property "$a" will not be proxied on the instance`).toHaveBeenWarned()
expect(`Property "_b" will not be proxied on the instance`).toHaveBeenWarned()
})
})