← Back to Vue.js Course | Chapter 4: Components | Lesson 2 of 7

Props

Props are custom attributes a parent uses to pass data down into a child component.

In this page:

  1. Props
Syntax
markup
app.component('child-name', {
  props: ['propName'],
  template: '<p>{{ propName }}</p>'
});

<child-name prop-name="value"></child-name>
<child-name :prop-name="expression"></child-name>

Props

Declare props in the child with the props option, then pass values with attributes. Use :prop to pass non-string values such as numbers and objects. Props flow one way, from parent to child, and the child should not mutate them.

Note: Kebab-case attributes in HTML map to camelCase props in JavaScript.

Example: Props

markup
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
  <user-badge name="Ada" :age="36" :skills="['math','code']"></user-badge>
</div>
<script>
  const app = Vue.createApp({});
  app.component("user-badge", {
    props: ["name", "age", "skills"],
    template: "<p>{{ name }} ({{ age }}) knows {{ skills.join(' & ') }}. Age type: {{ typeof age }}</p>",
  });
  app.mount("#app");
</script>
</body>
</html>

<!-- Output:
Rendered: Ada (36) knows math & code. Age type: number
-->
Live Example
Related Topics
Common Mistakes
  1. Mutating props inside the child
  2. Passing a number without v-bind so it becomes a string
  3. Forgetting to declare the prop
Chapter Summary
  • props declare accepted inputs
  • :prop passes non-string values
  • Data flows one way
  • Kebab-case maps to camelCase
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.