Home Web Front-end Vue.js Let's talk about the name attribute in vue3 and see how to use it!

Let's talk about the name attribute in vue3 and see how to use it!

Sep 14, 2022 pm 07:42 PM
vue vue.js vue3

If you use the <script setup> syntax in vue3 development, some additional processing is required for the name attribute of the component. The following article will talk to you about the usage skills of vue3 name attribute. I hope it will be helpful to you!

Let's talk about the name attribute in vue3 and see how to use it!

For vue@3.2.34 and above, use <script setup> When using a single file component, vue will automatically derive the name attribute based on the component file name. That is, a file named MyComponent.vue or my-component.vue, the name attribute is MyComponent, and when you explicitly define the name attribute in the component, the derived name will be overwritten. [Related recommendations: vuejs video tutorial]

The name attribute of the component can not only be used for <KeepAlive>, but also when using vuejs- When debugging code with the devtools plug-in, the corresponding component can also display its name attribute, which facilitates us to quickly locate the code and debug it. Explicitly defining the name attribute is a good practice.

In addition, if we want to display and define the name attribute in <script setup>, we need to add an additional script, that is:

<script>
  export default {
    name: "MyComponent"
  }
</script>

<script setup>
...
<script>
Copy after login

It's a bit cumbersome, so the community launched unplugin-vue-define-options to simplify this operation.

The steps are very simple:

1. Install

npm i unplugin-vue-define-options -D
Copy after login

2. Configure vite

// vite.config.ts
import DefineOptions from 'unplugin-vue-define-options/vite'
import Vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [Vue(), DefineOptions()],
})
Copy after login

3. If you use typescript to develop, you need to configure typescript support

// tsconfig.json
{
  "compilerOptions": {
    // ...
    "types": ["unplugin-vue-define-options/macros-global" /* ... */]
  }
}
Copy after login

After the installation and configuration is completed, you can use the defineOptions API provided to define the name attribute.

<script setup>
defineOptions({
  name: "MyComponent"  
})
<script>
Copy after login

So how does it do this?

For code using defineOptions:

<script setup>
import { useSlots } from 'vue'
  
defineOptions({
  name: 'Foo',
  inheritAttrs: false,
})
const slots = useSlots()
</script>
Copy after login

The compiled output is:

<script>
export default {
  name: 'Foo',
  inheritAttrs: false,
  props: {
    msg: { type: String, default: 'bar' },
  },
  emits: ['change', 'update'],
}
</script>

<script setup>
const slots = useSlots()
</script>
Copy after login

It can be found that this is the same as what we wrote above The script tags are the same, that is to say, unplugin-vue-define-options uses the vite plug-in to help us write two scripts during the compilation phase, simplifying our development.

(Learning video sharing: web front-end development, Basic programming video)

The above is the detailed content of Let's talk about the name attribute in vue3 and see how to use it!. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use echarts in vue How to use echarts in vue May 09, 2024 pm 04:24 PM

How to use echarts in vue

The role of export default in vue The role of export default in vue May 09, 2024 pm 06:48 PM

The role of export default in vue

How to use map function in vue How to use map function in vue May 09, 2024 pm 06:54 PM

How to use map function in vue

The difference between event and $event in vue The difference between event and $event in vue May 08, 2024 pm 04:42 PM

The difference between event and $event in vue

The role of onmounted in vue The role of onmounted in vue May 09, 2024 pm 02:51 PM

The role of onmounted in vue

The difference between export and export default in vue The difference between export and export default in vue May 08, 2024 pm 05:27 PM

The difference between export and export default in vue

What are hooks in vue What are hooks in vue May 09, 2024 pm 06:33 PM

What are hooks in vue

Onmounted in vue corresponds to which life cycle of react Onmounted in vue corresponds to which life cycle of react May 09, 2024 pm 01:42 PM

Onmounted in vue corresponds to which life cycle of react

See all articles