MOCKSTACKS
EN
Questions And Answers

More Tutorials









VueJs Props

Remarks

camelCase <=> kebab-case


When defining the names of your props, always remember that HTML attribute names are caseinsensitive. That means if you define a prop in camel case in your component definition...
Vue.component('child', {
 props: ['myProp'],
 ...
});

...you must call it in your HTML component as my-prop.

Examples


Passing Data from parent to child with props


In Vue.js, every component instance has its own isolated scope, which means that if a parent component has a child component - the child component has its own isolated scope and the parent component has its own isolated scope.

For any medium to large size app, following best practices conventions prevents lots of headaches during the development phase and then after while maintenance. One of such things to follow is that avoid referencing/mutating parent data directly from the child component. So then how do we reference the parent data from within a child component?

Whatever parent data is required in a child component should be passed to the child as props from the parent.
Use Case: Suppose we have a User database with two tables users and addresses with the following fields:

users Table
name name email
John Mclane (1) 234 5678 9012 john@dirhard.com
James Bond (44) 777 0007 0077 bond@mi6.com

addresses Table
block street city
Nakatomi Towers Broadway New York
Mi6 House Buckingham Road London

and we want to have three components to display corresponding user information anywhere in our app

user-component.js


export default{
 template:`<div class="user-component">
 <label for="name" class="form-control">Name: </label>
 <input class="form-control input-sm" name="name" v-model="name">
 <contact-details :phone="phone" :email="email"></contact-details>
 </div>`,
 data(){
 return{
 name:'',
 phone:'',
 email:''
 }
 },
}

contact-details.js


import Address from './address';
export default{
 template:`<div class="contact-details-component>
 <h4>Contact Details:</h4>
 <label for="phone" class="form-control">Phone: </label>
 <input class="form-control input-sm" name="phone" v-model="phone">
 <label for="email" class="form-control">Email: </label>
 <input class="form-control input-sm" name="email" v-model="email">
 <h4>Address:</h4>
 <address :address-type="addressType"></address>
 //see camelCase vs kebab-case explanation below
 </div>`,
 props:['phone', 'email'],
 data:(){
 return:{
 addressType:'Office'
 }
 },
 components:{Address}
}

address.js


export default{
 template:`<div class="address-component">
 <h6>{{addressType}}</h6>
 <label for="block" class="form-control">Block: </label>
<input class="form-control input-sm" name="block" v-model="block">
 <label for="street" class="form-control">Street: </label>
 <input class="form-control input-sm" name="street" v-model="street">
 <label for="city" class="form-control">City: </label>
 <input class="form-control input-sm" name="city" v-model="city">
 </div>`,
 props:{
 addressType:{
 required:true,
 type:String,
 default:'Office'
 },
 data(){
 return{
 block:'',
 street:'',
 city:''
 }
 }
}

main.js


import Vue from 'vue';
Vue.component('user-component', require'./user-component');
Vue.component('contact-details', require'./contact-details');
new Vue({
 el:'body'
});

index.html


...
<body>
 <user-component></user-component>
 ...
</body>

We are displaying the phone and email data, which are properties of user-component in contactdetails which doesn't have phone or email data.



Conclusion

In this page (written and validated by ) you learned about VueJs Props . What's Next? If you are interested in completing VueJs tutorial, your next topic will be learning about: VueJs Passing data as props.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.