Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define `name` and `inheritAttrs` in `<script setup>`?

Options API:

<script>
  import { defineComponent } from 'vue'

  export default defineComponent({
    name: 'CustomName', // 👈
    inheritAttrs: false, // 👈
    setup() {
      return {}
    },
  })
</script>

How to do that in <script setup>, is there an equivalent for name and inheritAttrs like defineProps and defineEmits?

<script setup>
  // 👉 how to define them here?
</script>
like image 925
Wenfang Du Avatar asked May 08 '21 08:05

Wenfang Du


People also ask

How do I use Vue scripts?

Add a script tag inside your Vue component template. Just add the script into the component template. But don't forget to add, type="application/javascript" to the script tag otherwise it will show an error during the build. However, if you have added document.

What is setup method in Vuejs?

setup can also return a render function which can directly make use of the reactive state declared in the same scope: js import { h, ref } from 'vue' export default { setup() { const count = ref(0) return () => h('div', count. value) } } Returning a render function prevents us from returning anything else.

How do I register a Vue component?

When we want to globally register a component in Vue, we need to do it in this file. All you have to do is import your component, as you usually would, and then register it using app. component . import { createApp } from 'vue' import App from './App.

What is <script setup> used for?

<script setup> is a compile-time syntactic sugar for using Composition API inside Single-File Components (SFCs). It is the recommended syntax if you are using both SFCs and Composition API. It provides a number of advantages over the normal <script> syntax:

How do I use slots and ATTRS inside <script setup>?

Usage of slots and attrs inside <script setup> should be relatively rare, since you can access them directly as $slots and $attrs in the template. In the rare case where you do need them, use the useSlots and useAttrs helpers respectively:

How do I expose properties in a <script setup> component?

Components using <script setup> are closed by default - i.e. the public instance of the component, which is retrieved via template refs or $parent chains, will not expose any of the bindings declared inside <script setup>. To explicitly expose properties in a <script setup> component, use the defineExpose compiler macro:


2 Answers

The <script setup> syntax provides the ability to express equivalent functionality of most existing Options API options except for a few:

  • name
  • inheritAttrs
  • Custom options needed by plugins or libraries

If you need to declare these options, use a separate normal <script> block with export default:

<script>
  export default {
    name: 'CustomName',
    inheritAttrs: false,
    customOptions: {},
  }
</script>

<script setup>
  // script setup logic
</script>

Compiled output:

<script>
  export default {
    name: 'CustomName',
    inheritAttrs: false,
    customOptions: {},
    setup() {
      // script setup logic
    },
  }
</script>
like image 136
Wenfang Du Avatar answered Oct 15 '22 02:10

Wenfang Du


there is a vite plugin vite-plugin-vue-setup-extend can resolve set component name.

config

// vite.config.ts
import { defineConfig } from 'vite'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'

export default defineConfig({
  plugins: [
    VueSetupExtend()
  ]
})

usage

<script lang="ts" setup name="CompName">
</script>
like image 36
YAN7 Avatar answered Oct 15 '22 01:10

YAN7