亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
What is useAttrs?
What is useSlots?
When to Use Them
Home Web Front-end Vue.js How to use useAttrs and useSlots in the Vue.js Composition API

How to use useAttrs and useSlots in the Vue.js Composition API

Oct 16, 2025 pm 01:11 PM
vue.js

useAttrs returns component attributes not declared as props, used for manually binding them to specific elements; useSlots provides an object of slot contents, allowing slots to be inspected and rendered by name. Both are often used to create higher-order or wrapper components that require precise control of properties and slots, and are used in conjunction with fragments in setup.

How to use useAttrs and useSlots in the Vue.js Composition API

In Vue 3's Composition API, useAttrs and useSlots are utility functions that give you access to the attributes and slots passed to a component. They're especially useful when creating wrapper components or higher-order abstractions where you need fine-grained control over how props, attributes, and content are handled.

What is useAttrs?

useAttrs returns an object containing the component's inherited attributes (like class , style , or custom event listeners) that are not declared as props. This is helpful when you want to manually apply these attributes to a specific element in your template — commonly used in wrapper or UI components.

By default, if a component has a single root element, attributes are automatically applied to it. But with setup() and fragments (multiple root nodes), automatic attribute fallthrough doesn't happen, so useAttrs gives you explicit control.

Example:

Suppose you have a custom input wrapper:

 import { useAttrs } from 'vue'

export default {
  setup() {
    const attrs = useAttrs()
    return {attrs}
  },
  template: `
    <div class="input-wrapper">
      <input v-bind="attrs" />
    </div>
  `
}

Now, when someone uses this component:

 <MyInput v-model="text" placeholder="Enter text" class="large" @focus="onFocus" />

All those attributes ( placeholder , class , @focus , and even v-model bindings) are passed through to the inner via v-bind="attrs" .

What is useSlots?

useSlots returns an object representing the slots passed to the component. Each key corresponds to a slot name (eg, default , header , footer ). Slot values ??are arrays of VNodes (virtual nodes), which you can render conditionally or pass down.

This is useful when writing renderless components or when you need to inspect or manipulate slot content before rendering.

Example:

A card component with optional header and footer:

 import { useSlots } from &#39;vue&#39;

export default {
  setup() {
    const slots = useSlots()
    return { slots }
  },
  template: `
    <div class="card">
      <header v-if="slots.header">
        <slot name="header" />
      </header>
      <main>
        <slot />
      </main>
      <footer v-if="slots.footer">
        <slot name="footer" />
      </footer>
    </div>
  `
}

You can now check if a slot exists using slots.header before rendering it — preventing empty wrappers.

When to Use Them

  • useAttrs : Use when building reusable wrappers (eg, form inputs, buttons, modals) and you want to forward attributes like events or classes to internal elements.
  • useSlots : Use when you need conditional rendering of slots or when working with dynamic slot logic (eg, layout components, tabs, or toolbars).

Both functions are fully reactive and update automatically when the parent passes new props or content.

Basically, useAttrs and useSlots give you low-level access to Vue's component composition features, making it easier to write flexible, reusable components without losing inheritance behavior.

The above is the detailed content of How to use useAttrs and useSlots in the Vue.js Composition API. 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 AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Hot Tools

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)

Hot Topics

Vue.js vs. React: Project-Specific Considerations Vue.js vs. React: Project-Specific Considerations Apr 09, 2025 am 12:01 AM

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

Is vue.js hard to learn? Is vue.js hard to learn? Apr 04, 2025 am 12:02 AM

Vue.js is not difficult to learn, especially for developers with a JavaScript foundation. 1) Its progressive design and responsive system simplify the development process. 2) Component-based development makes code management more efficient. 3) The usage examples show basic and advanced usage. 4) Common errors can be debugged through VueDevtools. 5) Performance optimization and best practices, such as using v-if/v-show and key attributes, can improve application efficiency.

Is Vue used for frontend or backend? Is Vue used for frontend or backend? Apr 03, 2025 am 12:07 AM

Vue.js is mainly used for front-end development. 1) It is a lightweight and flexible JavaScript framework focused on building user interfaces and single-page applications. 2) The core of Vue.js is its responsive data system, and the view is automatically updated when the data changes. 3) It supports component development, and the UI can be split into independent and reusable components.

Vue.js: Defining Its Role in Web Development Vue.js: Defining Its Role in Web Development Apr 18, 2025 am 12:07 AM

Vue.js' role in web development is to act as a progressive JavaScript framework that simplifies the development process and improves efficiency. 1) It enables developers to focus on business logic through responsive data binding and component development. 2) The working principle of Vue.js relies on responsive systems and virtual DOM to optimize performance. 3) In actual projects, it is common practice to use Vuex to manage global state and optimize data responsiveness.

Vue.js vs. React: Comparing Performance and Efficiency Vue.js vs. React: Comparing Performance and Efficiency Apr 28, 2025 am 12:12 AM

Vue.js and React each have their own advantages: Vue.js is suitable for small applications and rapid development, while React is suitable for large applications and complex state management. 1.Vue.js realizes automatic update through a responsive system, suitable for small applications. 2.React uses virtual DOM and diff algorithms, which are suitable for large and complex applications. When selecting a framework, you need to consider project requirements and team technology stack.

Vue.js and the Frontend Stack: Understanding the Connections Vue.js and the Frontend Stack: Understanding the Connections Apr 24, 2025 am 12:19 AM

Vue.js is closely integrated with the front-end technology stack to improve development efficiency and user experience. 1) Construction tools: Integrate with Webpack and Rollup to achieve modular development. 2) State management: Integrate with Vuex to manage complex application status. 3) Routing: Integrate with VueRouter to realize single-page application routing. 4) CSS preprocessor: supports Sass and Less to improve style development efficiency.

Vue.js in the Frontend: Real-World Applications and Examples Vue.js in the Frontend: Real-World Applications and Examples Apr 11, 2025 am 12:12 AM

Vue.js is a progressive JavaScript framework suitable for building complex user interfaces. 1) Its core concepts include responsive data, componentization and virtual DOM. 2) In practical applications, it can be demonstrated by building Todo applications and integrating VueRouter. 3) When debugging, it is recommended to use VueDevtools and console.log. 4) Performance optimization can be achieved through v-if/v-show, list rendering optimization, asynchronous loading of components, etc.

The Future of Vue.js and React: Trends and Predictions The Future of Vue.js and React: Trends and Predictions May 09, 2025 am 12:12 AM

The future trends and forecasts of Vue.js and React are: 1) Vue.js will be widely used in enterprise-level applications and have made breakthroughs in server-side rendering and static site generation; 2) React will innovate in server components and data acquisition, and further optimize the concurrency model.

See all articles