Example demonstration: Subcomponents: <template> <div> <h3>I am {{name}}, I am {{age}} years old, my hobby is {{hobby}}</h3>, {{flag}} </div> </template> <script> export default { name:'Cpn', // Simple reception /* props:['age','hobby','name'], */ // Declare the data to be received and restrict the received data when declaring props:{ name: { //Declare type type:String, //Declare whether it is required: true, // Declare the default value default:'default value' }, age:{ type:Number, require:true, default:18 }, hobby: type:String, require:false }, flag:{ type:Boolean, require:false } } } </script> Parent component: <template> <div id="app"> <!-- <cpn name='李明' age="22" hobby="playing ball"></cpn> <cpn name="Xiaohong" age="20" hobby="Playing the piano"></cpn> --> <cpn name='李明'></cpn> <cpn hobby="Programming" :flag="flag"></cpn> <!--Note: If you want to pass the data in the current component data to the child component, you need to pass it in the form of v-bing: variable name = "variable name". If the data passed is not in data, there is no need to add the binding instruction, that is, v-bind (can be abbreviated as:) --> <button @click="changeFlag">Switch</button> </div> </template> <script> import Cpn from './components/Cpn.vue' export default { components: { Cpn }, name: "App", data() { return { flag:false } }, methods: { changeFlag(){ console.log(this.flag) this.flag=!this.flag; console.log(this.flag) } }, } </script> Running the above program, we can see that when we switch a value by clicking the button of the parent component, the value received by the child component will also change accordingly. There are two ways for a child component to receive data from its parent component:
This is the end of this article about the usage of props in Vue. For more information about the usage of props in Vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: The implementation principle of Tomcat correcting the JDK native thread pool bug
>>: CSS horizontal progress bar and vertical progress bar implementation code
On a whim, I wrote a case study of a small ball b...
This article example shares the specific code of ...
Table of contents 1. Component Communication 1. P...
This article shares the detailed steps of install...
The replace statement is generally similar to ins...
Table of contents forEach() Method How to jump ou...
ChunkFive Free Typefamily Cuprum JAH I Free font Y...
This article example shares the specific code of ...
Use OSS to upload pictures or attachments in vue ...
Use of v-on:clock in Vue I'm currently learni...
Format Encoding 1. Please set the page width with...
This article mainly introduces common strategies ...
Table of contents 1. Ternary operator judgment 2....
Primitive values -> primitive types Number S...
Table of contents 1. Generate AST abstract syntax...