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

Props का परिचय

Props custom attributes हैं जिनसे एक parent data को एक child component में नीचे pass करता है।

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

props option से child में props declare करें, फिर attributes से values pass करें। numbers और objects जैसी non-string values pass करने के लिए :prop इस्तेमाल करें। Props एक तरफ flow करते हैं, parent से child तक, और child को इन्हें mutate नहीं करना चाहिए।

Note: HTML में Kebab-case attributes JavaScript में camelCase props में map होते हैं।

उदाहरण: 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/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. child के अंदर props mutate करना
  2. v-bind के बिना एक number pass करना ताकि यह एक string बन जाए
  3. prop declare करना भूल जाना
चैप्टर सारांश
  • props accepted inputs declare करता है
  • :prop non-string values pass करता है
  • Data एक तरफ flow करता है
  • Kebab-case camelCase में map होता है
🔒

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.