i find no documentation online or examples, but which is the correct way to pass multiple props to component?
here what i have tried:
in HTML
<component:prop1="data1" :prop2="data2"></component>
in component.js
props: ['prop1','prop2'],
but obiusly don't work...
To specify the type of prop you want to use in Vue, you will use an object instead of an array. You'll use the name of the property as the key of each property, and the type as the value. If the type of the data passed does not match the prop type, Vue sends an alert (in development mode) in the console with a warning.
Bookmark this question.
In Vue, props (or properties), are the way that we pass data from a parent component down to it's child components. When we build our applications out of components, we end up building a data structure called a tree.
despite this been a little old, I'd like to contribute. If you want to pass a bunch of propeties "at once" together you could use "v-bind"
Let's say your component has a bunch of props:
props: ['age', 'year', 'date']
Instead the regular way below:
<MyAwesomeComponent :age="age" :year='year' :date='today' />
It's also possible to do:
<MyAwesomeComponent v-bind="groupedProps" />
been "groupedProps" the following object:
groupedProps: {age:134, year:2153, today: new Date()}
...v-bind="groupedProps"...
the behavior is described in the vue docs here
You can pass multiple props to components like this. In parent component:
<template>
<div id="app">
<child-component :propA=propa :propB=propb></child-component2>
</div>
</template>
<script>
import ChildComponent from './components/comp2'
export default {
name: 'app',
components: {
ChildComponent
},
data () {
return {
propa : 65,
propb : 'Nitin'
}
}
}
</script>
<style></style>
In child component:
<template>
<div>
{{propA}}
{{propB}}
</div>
</template>
<script>
export default {
props: {
propA: Number,
propB: String
}
}
</script>
<style lang="css">
</style>
Reference:
Vue Prop-Validation
EDIT:
More info and example references can be found at:
Passing-Data-to-Child-Components-with-Props
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With