Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use props in Vue

Shulou Source: shulou.com Published: 2022-06-02 11:35:56 09月12日 Update

This article mainly shows you "how to use props in Vue", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use props in Vue" this article.

Foreword:

In Vue, through props, originally isolated components can be concatenated, that is, child components can receive the data passed by the parent component, for example, if the child component wants to reference the data of the parent component, then declare a variable in the props, and this variable can refer to the data of the parent element.

Example demonstration:

Subcomponents:

I am {{name}}, this year {{age}} years old, hobby: {{hobby}}, {{flag}} export default {name:'Cpn', / / simple receive / * props: ['age','hobby','name'], * / / declare the data to be received Restrict the data received when declaring: props: {name: {/ / declare type type:String, / / declare whether require:true is required, / / declare default value default:' default'}, age: {type:Number Require:true, default:18}, hobby: {type:String, require:false}, flag: {type:Boolean, require:false}

Parent component:

Toggle import Cpn from'. / components/Cpn.vue'export default {components: {Cpn}, name: "App", data () {return {flag:false}}, methods: {changeFlag () {console.log (this.flag) this.examples; console.log (this.flag)}},}

Running the above program shows that when we switch a value by clicking the button of the parent component, the value received by the child component changes accordingly.

There are two ways for a child component to receive data from a parent component:

Mode 1: simply receive, just give the name of the variable you want to receive

Mode 2: specific reception, selectively indicating the data type to each received variable, whether it can be empty, the default value

The above is all the contents of the article "how to use props in Vue". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Components data variables content methods articles types switching learning help that is elements prefaces originals names instances buttons easy to understand more organized Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno Redmi Xiaomi Shulou Tech Info