Pass object as prop on Vue

Javascriptvue.js

Javascript Problem Overview


How do you go on passing objects as props on vue? I would imagine this would be a simple task but apparently not.

I have the following code on a .vue file:

<template>
  <div id="scatter"></div>
</template>

<script>
export default {
  props: {
    data: {
      type: Object,
      default: () => ({}),
    },
  },
  mounted() {
    console.log(this.data);
  },
};
</script>

On html I try to pass the data props as follows :

<component :data="{x:1}"></component>

When I try log it into the console the result is only an empty observer object.

Javascript Solutions


Solution 1 - Javascript

I believe the problem is somewhere else in your code as passing an object as a prop is as simple as you imagine:

// register the component
Vue.component('component', {
  props: {
  	data: {
    	type: Object
    }
  },
  template: '<div>Data: {{data}}</div>',
  mounted: function () {
  	console.log(this.data)
  }
})

new Vue({
  el: '#example'
})

HTML:

<div id="example">
  <component :data="{x:1}"></component>
</div>

Here's a fiddle showing it in action: https://jsfiddle.net/tk9omyae/

Solution 2 - Javascript

Edit: After my initial answer and creating a JsFiddle, I am not sure why the behavior you described is happening. It works when narrowed down to the use case:

<script>
    export default {
       props: {
           ok: {
               type: Object,
               default: () => ({}),
           },
           data: {
               type: Object,
               default: () => ({})
           }
       }
     },
     mounted () { 
         console.log(this.data) // {x:1}
         console.log(this.ok) // {x:1}
     }
    }
</script>

And the HTML:

<component :ok="{x:1}" :data="{x:1}"></component>

Here is a JsFiddle that demonstrates the behavior: https://jsfiddle.net/mqkpocjh/

Solution 3 - Javascript

v-bind="yourObject"

Should do on <my-component v-bind="yourObject"><my-component>

Not sure about <component></component>. Still digging into Vue. Try and let us know.

Solution 4 - Javascript

100% Working Passing object is very simple way from one component to another component.

> Child component Code simple code where StokDetail is an object passing from
> other component

  export default {  
     props: {
      StockDetail: {
         type: Object,
         default: (()=>{})
      },
  },
    created:function(){
        console.log(this.StockDetail);
    }
 }
 </script> ``` 
> Pass from Parent Component
>
<stock-detail-component v-bind:stock-detail="model.StockDetail"></stock-detail-component>

HTML attributes are case-insensitive, so when using non-string templates, camelCased prop names need to use their kebab-case (hyphen-delimited) equivalents: 
such as  StockDetail = stock-detail

you can see in this below snapshot





 


  [1]: https://i.stack.imgur.com/tIofC.png

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDiego GallegosView Question on Stackoverflow
Solution 1 - JavascriptJamie CurnowView Answer on Stackoverflow
Solution 2 - JavascriptexclsrView Answer on Stackoverflow
Solution 3 - JavascriptHalfWebDevView Answer on Stackoverflow
Solution 4 - JavascripttwostepdevelopersView Answer on Stackoverflow