A simple understanding of _javascript techniques of props attributes in Vue

Source: Internet
Author: User

This article is an example for us to analyze the attributes of props in Vue, for your reference, the specific contents are as follows

Passing Data using Props

The scope of the component instance is isolated. This means that the parent component's data cannot and should not be referenced directly within the subassembly's template. You can use props to pass data to a subassembly.

"Prop" is a field of component data that is expected to pass from the parent component. Subcomponents need to explicitly declare props with the props option:

Vue.component (' child ', {
 //Declaration props
 Props: [' msg '],
 ///prop can be used in the template
 //can be set with ' this.msg '
 Template: ' <span>{{msg}}</span> '
})

Then pass it to a normal string:

<child msg= "Hello!" ></child>

Example

Wrong wording:

<! DOCTYPE html>
 
 

Correct wording:

<! DOCTYPE html>
 
 

Props incoming multiple data (order issue)

First type:

Html

<div id= "App1" >
<child msg= "hello!" ></child>
<child nihao= "hello1!" ></child>
<child nisha= "hello2!" ></child>
</div>

Js

 Vue.config.debug = true;
 Vue.component (' child ', {
 //Declare the props
 props: [' msg ', ' Nihao ', ' Nisha '],
 //The prop can be used inside Templates, and would also
 //be set as ' this.msg '
 Template: ' <span>{{msg}}{{NIHAO}}{{NISHA}}</SPAN&G t; ',
 /*data:function () {return
 {
  msg: ' Boy '
 }
 }*/
 });
 var vm = new Vue ({
 el: ' #app1 '
 })

Result: hello! hello1! hello2!

The second type:

Html

<div id= "App1" >
<child msg= "hello!" ></child>
 <child nihao= "hello1!" ></child>
 <child nisha= "hello2!" ></child>
</div>

Js

 Vue.config.debug = true;
 Vue.component (' child ', {
 //Declare the props
 props: [' msg ', ' Nihao ', ' Nisha '],
 //The prop can be used inside t Emplates, and would also
 //be set as ' this.msg '
 Template: ' <span>123{{msg}}{{nihao}}{{nisha}}</span& gt; ',
 /*data:function () {return
 {
  msg: ' Boy '
 }
 }*/
 });
 var vm = new Vue ({
 el: ' #app1 '
 })

Result: 123hello! 123hello1! 123hello2!

The third type:

Html

<div id= "App1" >
<child msg= "hello!" ></child>
<child nihao= "hello1!" ></child>
 <child nisha= "hello2!" ></child>
</div>

Js

 Vue.config.debug = true;
 Vue.component (' child ', {
 //Declare the props
 props: [' msg ', ' Nihao ', ' Nisha '],
 //The prop can be used inside t Emplates, and would also
 //be set as ' this.msg '
 Template: ' <span>{{msg}}{{nihao}}{{nisha}}123</span& gt; ',
 /*data:function () {return
 {
  msg: ' Boy '
 }
 }*/
 });
 var vm = new Vue ({
 el: ' #app1 '
 })

Result: hello! 123 hello1! 123 hello2!123

The fourth kind:

Html

<div id= "App1" >
<child msg= "hello!" ></child>
<child nihao= "hello1!" ></child>
<child nisha= "hello2!" ></child>
</div>

Js

 Vue.config.debug = true;
 Vue.component (' child ', {
 //Declare the props
 props: [' msg ', ' Nihao ', ' Nisha '],
 //The prop can be used inside t Emplates, and would also
 //be set as ' this.msg '
 Template: ' <span>{{msg}}123{{nihao}}{{nisha}}123</ Span> ',
 /*data:function () {return
 {
  msg: ' Boy '
 }
 }*/
 });
 var vm = new Vue ({
 el: ' #app1 '
 })

Result: hello! 123 123hello1! 123hello2!

Conclusion:

Passing in multiple data in props is if additional elements or characters are added to the parent component's template class:
1-Add in front-each subassembly is rendered in front of it plus

2-Add on the last side-each subassembly will be rendered with the following

3-Join in the middle-he is preceded by a subassembly followed by a subassembly followed by a sub component

Reference: http://cn.vuejs.org/guide/components.html

This article has been organized into the "Vue.js front-end component Learning course", welcome to learn to read.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.