<script setup> should register components by name #273
Replies: 6 comments 3 replies
-
It's explicitly not designed that way: https://github.com/vuejs/rfcs/blob/script-setup-2/active-rfcs/0000-script-setup.md#exposing-components-and-directives |
Beta Was this translation helpful? Give feedback.
-
Hi. Did you find a solution for the component name to be used in <script setup>? |
Beta Was this translation helpful? Give feedback.
-
a solution: <template>
<button :class="['text' + size]">size: {{ size }}</button>
</template>
<script lang="ts">
export default { name: "test-btn" };
</script>
<script setup lang="ts">
import { defineProps } from "vue";
import type {PropType} from 'vue'
defineProps({
size: {
type: String as PropType<IButtonSize>,
default: "base"
}
})
</script> |
Beta Was this translation helpful? Give feedback.
-
I think this also makes it impossible to get Consider this case from here: https://stackoverflow.com/a/65760822/10706046
If you have a MoviesList.vue file the autogenerated name |
Beta Was this translation helpful? Give feedback.
-
If you’re using Vue Router and have named each route, you can try this: routes.forEach(route => route.component().then(mod=>mod.default.name=route.name)); hope this helps 😃 |
Beta Was this translation helpful? Give feedback.
-
have to use a plugin: import { Plugin } from 'vite'
export const setupName = (): Plugin => {
return {
name: 'setup-add-name',
enforce: 'pre',
transform(code, id) {
if (!id.endsWith('.vue')) {
return { code, map: null }
}
const match = code.match(/<script\s+.*name=('|")(.+?)('|")/)
const lang = code.match(/<script\s+.*lang=('|")(.+?)('|")/)
if (match) {
return {
code: code + `<script lang="${lang?.[2]}">export default {name: "${match[2]}"}</script>`,
map: { mappings: '' },
}
}
return { code, map: null }
},
}
}``` |
Beta Was this translation helpful? Give feedback.
-
Previous comment in RFC pull: #227 (comment)
With
<script setup>
it is impossible to use<component :is="">
with component name as :is.But it worked fine in v2 and v3 with normal composition api. So I think it should also work with <script setup> syntax and I consider this as a bug. Even if this behavior will be not changed, then it at least should be clarified somewhere in RFC and later docs :)
Beta Was this translation helpful? Give feedback.
All reactions