Script setup picks a name that creates warnings
Vue version
3.2.26
Link to minimal reproduction
Steps to reproduce
Put a script setup component inside a file named button.vue
or nav.vue
or any name that matches an existing html element.
Use it in your application, for example as a local component:
<template>
<my-button>Hello</my-button>
</template>
<script setup>
import MyButton from "./button.vue"
</script>
Or as a globally registered component:
import MyButton from "./button.vue"
app.component("my-button", MyButton);
What is expected?
Works without issue, no warning on console
What is actually happening?
Many warnings on the console:
Do not use built-in or reserved HTML elements as component id: button
System Info
No response
Any additional comments?
This is caused by the recently introduced inferred naming of script setup
components.
Vue picks the filename as a default component name, as can be seen in the repro link:
const __sfc__ = {
name: 'button',
// ...
}
This name is then validated against reserved names and that's why a warning is raised.
In this context, the name is actually meaningless so here are two suggestions:
- Is it even required to validate the component
name
property? The same code validates the name used as a tag inapp.component('this-is-validated', Button)
, which is good, but I'm not sure if the internal component name needs validation? - The compiler can pick a different default name if the filename clashes with a native element. E.g. if the file is named
button.vue
, generatename: 'vue-button'
instead; or no name at all, as previously.
Workarounds for now:
- never use a reserved name as your SFC filename. (this can be annoying after-the-fact, e.g. we have our own component library and to be consistent we would need to rename many files, breaking all imports).
- export an explicit name in a
<script>
block to avoid inferred name.