Vue.js Prop Passing Details
Last Updated :
23 Jul, 2024
Vue.js Props are used to pass data to the component. For passing details to props we have to first declare props. To declare the prop name we choose a name that is more descriptive of the value that it holds. In this article, we will see the Prop passing the details in Vue.js. The following are the different properties of the prop:
We will explore the above-mentioned Props in Vue.js in detail, along with understanding their basic implementation through the illustration.
Prop Name Casing
- We have different types of conventions for writing the names of props such as camelCasel, kebab-case, PascalCase, etc. We generally use camelCase to long prop name because it allows us to directly reference them in dom because they are valid JavaScript identifiers.
props : { newProp1 : String // cameCase example }
- We can use the Kebab case to pass the details of the props to the component.
<Student student-name='Sam Snehil' />
- We use PascalCase for component tags which differentiates the component tag from the native tag which helps readers to understand the code.
<StudentClass /> // component tag
static vs. Dynamic Props
- We have different ways to pass details to Prop Sometimes it can be static and sometimes it can be dynamic. Static props are a value that is fixed throughout the existence of a web page. Like below:
<Student student-name='Sam' />
- Dynamic value is bound with a variable if the variable is changed props data will also be changed.
<Student :student-name='variable' />
Passing different values
We can pass all possible data types to props. Some of them are:
<Student Age=23 /> // Static
<Student :Age='age' /> // Dynamic
<Student name='Same /> // Static
<Student :name='name' /> // Dynamic
<Student StudentPass='false' />
<Student :StudentPass="stud1.pass" />
<Student student-marks='[89, 38, 83]' />
<Student :student-marks="stud1.marks" />
<Students student1="{ name: 'sam', age: '23' }" />
<Students :student1="students.stud1" />
Binding Multiple Properties Using an Object
If we want to pass all pros in one object we can bind all properties using Objects and v-bind directive
// One props is name and one age we want to pass it together
let stud1 = { name : 'Sam', age: 23 }
// It is equivalent to <Student :name="stud1.name" :age="stud1.age" />
<Students v-bind="sutd1" />
Syntax
<ComponentName props = 'PropsValue' />
Parameter
- props: It name of the props to which we pass data.
- PropsValue: It is the details we pass to prop.
Example 1: In this example, we will pass static details to props and view them using name case camel case, and kebab-case.
HTML
<!-- App.vue -->
<template>
<h1>GeeksforGeeks</h1>
<Student student-name="Sam"
student-batch="5th"
student-age=23
student-course="Mtech" />
</template>
<script>
import Student from './components/Student.vue';
export default {
name: 'App',
components: {
Student,
},
}
</script>
<style>
#app {
text-align: center;
margin-top: 60px;
}
h1 {
color: rgb(40, 212, 103)
}
</style>
HTML
<!-- Student.vue -->
<template>
<h2>Click on botton to view the props</h2>
<button v-on:click.left="data = !data">
Show
</button>
<h2 v-if="data">Name: {{ studentName }}<br />
Batch: {{ studentBatch }}<br />
Course: {{ studentCourse }}<br />
Age: {{ studentAge }}<br />
</h2>
</template>
<script>
export default {
name: 'StudentL',
props: {
studentName: String,
studentBatch: String,
studentAge: String,
studentCourse: String
},
data() {
return { data: 0 };
}
}
</script>
Output:
Example 2: In this example, we will pass dynamic details to props and view them.
HTML
<!-- App.vue -->
<template>
<h1>GeeksforGeeks</h1>
<Student v-bind='student[i]' />
<button @click="i=(i+1)%2">
change
</button>
</template>
<script>
import Student from './components/Student.vue';
export default {
name: 'App',
components: {
Student,
},
data() {
return {
student: [
{ name: 'Sam',
Age: 23,
Course: "Btech",
Batch: '3rd' },
{ name: 'Sam2',
Age: 23,
Course: "Mtech",
Batch: '4rd' }],
i: 0
};
}
}
</script>
<style>
#app {
text-align: center;
margin-top: 60px;
}
h1 {
color: rgb(40, 212, 103)
}
</style>
HTML
<!-- Student.vue -->
<template>
<h2>Click on botton to view the props</h2>
<button v-on:click.left="data = !data">
Show
</button>
<h2 v-if="data"> Name : {{ name }} <br />
Course : {{ Course }} <br />
Age : {{ Age }}<br />
Batch : {{ Batch }}</h2>
</template>>
<script>
export default {
name: 'StudentL',
props: {
name: String,
Course: String,
Age: Number,
Batch: String,
},
data() {
return { data: 0 };
}
}
</script>
Output:
Similar Reads
Vue.js Prop One-Way Data Flow
In VueJS, all the props are bonded between the child and properties is one-way-down. When we update the prop in the parent component, it is updated to all the child components, but the reverse does not happen and VueJS warns the use. This is used as protection from altering the state of a parent-by-
4 min read
Props vs Data in Vue.js
Props in Vue.jsProps in Vue.js are custom attributes that allow data to be passed from a parent component to a child component. This enables the parent component to communicate with its child components, providing them with data or functionality. In Vue.js, props are used to pass data from parent co
3 min read
Vue.js Listening to Events
Vue.js provides the different events that are used to handle user interactions and component communication. To listen to those DOM events, Vue.js provides a v-on directive. We can use the shortened @ symbol in place of the v-on directive. Listening to Events is the programming concept of waiting for
4 min read
Vue.js Props Declaration
Vue.js Props Declaration facilitates the declaration of props explicitly for the Vue components. With this, Vue can identify how external props are passed to the component that should be treated as fall-through attributes. In other words, Props of Vue components is a configuration for the transfer o
3 min read
Vue.js Passing Data to Child Components with Props
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
Vue.js Reusing Components
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
4 min read
Vue.js Placeholder using filters
Vue.JS filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned val
3 min read
Vue.js v-pre Directive
The v-pre directive is a Vue.js directive used to skip compilation for this element and all its children. You can use this for displaying raw mustache tags. First, we will create a div element with id as app and let's apply the v-pre directive to an element. Further, we can use a heading element to
1 min read
REST API Call to Get Location Details in Vue.js
In this article, we will know the REST API call to get the location details in VueJS, along with understanding its implementation through the examples. VueJS is one of the best frameworks for JavaScript like ReactJS. The VueJS is used to design the user interface layer, it is easy to pick up for any
7 min read
Vue.js Props
Vue.js Props is an attribute to the configuration of the component. It is used to pass the data to the component to work dynamically. The props configuration defines the type of data it can receive from different components of Vue. Table of Content Props DeclarationProp Passing DetailsOne-Way Data F
4 min read