Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue3 props Property

Vue3 props Property


Vue3 Options API Vue3 Options API

props is a way to pass data between components.

In Vue.js, the parent component can pass data to the child component via props . After receiving this data, the child component can render based on it or perform other operations.

An Analogy

You can think of props as a postman. The parent component is the sender and the child component is the recipient. The parent passes data (a letter) to the child via props(the postman). After receiving the data, the child component can process it based on its content.


How to Use props?

Using props in Vue.js is very simple. Below we use an example to illustrate how to use props.

Example: Passing and Receiving props

Suppose we have a parent component ParentComponent and a child component ChildComponent. We want the parent component to pass a message to the child component.

1. Parent Component (ParentComponent.vue)

Example

2. Child Component (ChildComponent.vue)

Example

Explanation

  1. Parent component: In the parent component, we pass the :message="parentMessage" data to the child component via parentMessage .parentMessage is a variable defined in the parent component with the value 'Hello from Parent!'.

  2. Child component: In the child component, we use props to receive the data passed from the parent component.message is a props, whose type is Stringand set to required: true, indicating this props is required.

  3. Rendering: After the child component receives the message , it renders it into the template, displaying

    Hello from Parent!

    .


props Types and Validation

In Vue.js, you can specify types and validation rules for props to ensure the passed data meets expectations.

Example: Types and Validation

Example

props

:

{

  message

:

{

    type

:

String

,

    required

:

true

 

}

,

  age

:

{

    type

:

Number

,

   

default

:

18

 

}

,

  hobbies

:

{

    type

:

Array

,

    validator

:

function

(

value

)

{

     

return

value.

length

0

;

   

}

 

}

}

Explanation

  1. **type**: Specifies the props data type. Can be String, Number, Boolean, Array, Object , etc.

  2. **required**: Specifies the props whether it is required. If set to true, the parent component must pass this props, otherwise a warning is thrown.

  3. **default**: Sets a default value for props . If the parent component does not pass this props, the child component will use the default value.

  4. **validator**: A custom validation function used to validate props value meets specific conditions.


Notes

  1. One-Way Data Flow: In Vue.js,props is one-way, meaning data can only flow from the parent component to the child component. The child component should not directly modify the props value. If modification is needed, it can notify the parent component to make the change by emitting an event.

  2. Naming Convention:props 's naming is recommended to use camelCase, for example userName. When passing props in the parent component, kebab-case is recommended, for example user-name.

  3. Dynamic props: You can dynamically bind v-bind or : using props, for example :message="parentMessage".


Vue3 Options API Vue3 Options API