feat(runtime-core): enhance the warning when not explicitly declare the variable

This commit is contained in:
曹仲 2024-10-25 11:33:42 +08:00
parent 848e27cf28
commit d516036ee2
2 changed files with 24 additions and 1 deletions

View File

@ -526,7 +526,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
`but is not defined on instance.`,
)
}
} else if (__DEV__ && !__TEST__ && !(key[0] === '$' || key[0] === '_')) {
} else if (__DEV__ && !__TEST__ && !isReservedPrefix(key[0])) {
warn(
`Property ${JSON.stringify(key)} was accessed during render ` +
`but is not defined on instance.`,

View File

@ -311,4 +311,27 @@ describe('compiler + runtime integration', () => {
app.mount(root)
expect(root.innerHTML).toBe('<div>60000000100000111</div>')
})
test('Warning when use undeclared variable in template', () => {
const app = createApp({
template: `
<div @click="handler">handler</div>
<div>dddd{{dddd}}</div>
<div>no warn: {{message}}</div>
`,
setup() {
return {
message: 'hello',
}
},
})
const root = document.createElement('div')
app.mount(root)
expect(
`[Vue warn]: Property "handler" was accessed during render but is not defined on instance.`,
).toHaveBeenWarned()
expect(
`[Vue warn]: Property "dddd" was accessed during render but is not defined on instance.`,
).toHaveBeenWarned()
})
})