Subscribe on changes!

Add inferred component name(__name) to <script>

avatar
Jul 26th 2022

What problem does this feature solve?

compiler-sfc adds __name property to <script setup>, but it does not adds __name property to regular <script>. Is it appropriate to add __name to all types of <script> so I can get inferred name if one component does not have a name property.

What does the proposed API look like?

In all conditions, I can get __name:

export default {
  mounted() {
    console.log(this.$options.__name)
  }
}
<script setup>
const inst = getCurrentInstance().proxy
onMounted(() => {
  console.log(inst.$options.name)
})
</script>
avatar
Nov 10th 2023

If you only need the component name, you can retrieve it using the following code within the setup function: getCurrentInstance().proxy.$options.name. When you use Single-File Components, the component already infers its own name from the filename.