Vue.js / Nuxt.js

Vue Components

Component lifecycle, props, events, slots, scoped styles, component communication, dynamic components

20 interview questions·
Junior
1

What is a Vue component?

Answer

A Vue component is a reusable Vue instance with a name. It encapsulates HTML, CSS and JavaScript into an independent logical unit. Components allow dividing the user interface into reusable and maintainable pieces. Each component has its own template, logic and styles, facilitating code organization and reusability.

2

How to declare props in a Vue 3 component with script setup?

Answer

In Vue 3 with script setup, defineProps() is a compiler macro that allows declaring props without explicit import. This macro automatically generates the necessary code and exposes props to the template. It supports TypeScript and JavaScript type validation, making the code more concise and type-safe than the Options API approach.

3

What is the function of the onMounted() hook in a component lifecycle?

Answer

The onMounted() hook executes after the component has been mounted to the DOM. This is the ideal moment to perform operations that require DOM access, like initializing third-party libraries, fetching data via API, or directly manipulating DOM elements. At this stage, the template is rendered and refs are accessible.

4

How does a child component emit an event to its parent component?

5

What are slots used for in a Vue component?

+17 interview questions

Master Vue.js / Nuxt.js for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free