Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use props in Vue

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report