Vue3 Fundamentals · Components, Props & Events

Passing data with props

Passing data with props

A component that hard-codes its own text can only ever say one thing. To reuse it, a parent needs a way to hand it data.

Vue calls that data props. You declare the props a component accepts with defineProps, then read them in the template like any other value:

<script setup>
defineProps(['label']);
</script>

<template>
  <h1>Hello, {{ label }}!</h1>
</template>

A parent passes a prop as an attribute: <Greeting label="Vue" />. Vue matches the attribute to the declared prop and makes its value available by name. defineProps is a compiler macro, so you never import it.

Declaring the prop is what makes the component reusable: the same component renders different text for different parents. Without the declaration, the attribute is not a prop at all — Vue treats it as a stray attribute and passes it down to the root element instead of into your template.

The Test mounts your component with a label prop and checks the heading it renders.

Challenge

The component always says the same thing. Make it accept data: export an App component from App.vue that declares a label prop and renders it in a level-one heading as Hello, {{ label }}!.

The Test mounts your component with label set to Vue and expects the heading to read Hello, Vue!. The starter hard-codes a different greeting, so the Test fails. Declare the prop and use it in the template.

App.vue
<script setup>
const greeting = 'Hello, stranger!';
</script>

<template>
  <h1>{{ greeting }}</h1>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.