Ефим Маргин
Ефим Маргин

Reputation: 27

how to pass props correctly?

I can't pass multiple elements. How can I do this?

export default {
  props: {
    elem: {
      type: Object,
      required: true,
    },
    whichScreen: whichScreen
  },

Upvotes: 0

Views: 103

Answers (2)

Shreekanth
Shreekanth

Reputation: 859

Here is how to use props in the child component.

Parent component:-

<template>
  <div id="app">
    <child data="Shreekanth is here"/>  // Child component and Ddta attribute passing to child component
  </div>
</template>

<script>
import Child from './components/Child.vue' // Child component imported

export default {
  name: 'App',
  components: {
    Child
  }
}
</script>

Child component:-

<template>
    <div>
        <div>{{data}}</div> // Used data attribute used in child template
    </div>
</template>

<script>
export default {
    name: 'Home',
    props: {
        data: String // How data attribute registed in child component 
    }
}
</script>

Upvotes: 0

Chathuranga K
Chathuranga K

Reputation: 226

You can add the whichScreen prop like below :

export default {
   props : {
       elem : {
           type: Object,
           required: true,               
       },
       whichScreen : String
   }, 
}

you can pass props to the component like below :

<my-component :elem="{ 'key' : 'value' }" :which-screen="'Screen 1'"></my-component>

Complete Working Sample :

Vue.component('my-component', {
  template: '#tmpl-my-component',
  props : {
    elem : {
      type: Object,
      required: true,               
    },
    whichScreen : String
  },
});

new Vue({
  el: '#app'
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <my-component :elem="{ 'key' : 'value' }" :which-screen="'Screen 1'"></my-component>
</div>


<template id="tmpl-my-component">
  <div>
    <div><h4>Prop `elem` :</h4> {{elem}}</div>
    <div><h4>Prop `whichScreen` :</h4> {{whichScreen}}</div>
  </div>
</template>

Upvotes: 1

Related Questions