Skip to content

fix(runtime-core): fix onOnce error #8341

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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: 4 additions & 0 deletions packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ describe('component: emit', () => {
'test-event': null,
fooBar: null,
FooBaz: null,
once: null,
}
expect(isEmitListener(options, 'onClick')).toBe(true)
expect(isEmitListener(options, 'onclick')).toBe(false)
Expand All @@ -428,6 +429,9 @@ describe('component: emit', () => {
expect(isEmitListener(options, 'onFooBar')).toBe(true)
// PascalCase option
expect(isEmitListener(options, 'onFooBaz')).toBe(true)
// #8342
expect(isEmitListener(options, 'onOnceOnce')).toBe(true)
expect(isEmitListener(options, 'onOnce')).toBe(true)
})

test('does not emit after unmount', async () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,11 @@ export function isEmitListener(
if (__COMPAT__ && key.startsWith(compatModelEventPrefix)) {
return true
}

key = key.slice(2).replace(/Once$/, '')
key = key.slice(2)
// #8342
// e.g. emits: { once: null } -> onOnce、ononce
// Does not require regular expression processing
key = key.toLowerCase() === 'once' ? key : key.replace(/Once$/, '')
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this?

Suggested change
key = key.toLowerCase() === 'once' ? key : key.replace(/Once$/, '')
key = key.replace(/Once$/, '') || key

While it is less explicit, it avoids the toLowerCase() call.

return (
hasOwn(options, key[0].toLowerCase() + key.slice(1)) ||
hasOwn(options, hyphenate(key)) ||
Expand Down