Data Mastery
Data Mastery

Reputation: 2085

VueJS - unable to pass props from parent to child component

This is my parent:

<template>
    <Chart :type="bar" :data="[2,15,3,4,5,1,2,4,10,11]"></Chart>
</template>

<script>
import Chart from "./Chart"

    export default {
     components: {
        Chart
    },
  }

</script>

This is my child component:

<template>
    <highcharts :options="chartOptions"></highcharts>
</template>

<script>
import {Chart} from 'highcharts-vue'

    export default {
      props: ["data", "type"],
      components: {
      highcharts: Chart 
    },
    data() {
      return {
        chartOptions: {
          chart: {
            type: this.type
          },
          series: [{
            data: this.data, // sample data
            name: "Test Series",
            color: "blue"
          }]
        }
      }
  }
  }
</script>

I am able to pass the data with the props, but not the type. When I change type: this.type to type: "bar" it works as expected, so there is no error in the Code itself.

I get the following warning:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Property or method "bar" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

What am I doing wrong?

Upvotes: 2

Views: 740

Answers (2)

Alessandro Filira
Alessandro Filira

Reputation: 164

You use :type="bar" so Vue looking for a variable o getter that is called "bar". If you want to pass the value as string you should remove ":" before "type".

<template>
    <Chart type="bar" :data="[2,15,3,4,5,1,2,4,10,11]"></Chart>
</template>

Upvotes: 1

Majed Badawi
Majed Badawi

Reputation: 28404

You are trying to dynamically bind the prop "type" to bar as if the last is a variable. If you want to pass it as a String do this:

<Chart type="bar" :data="[2,15,3,4,5,1,2,4,10,11]"></Chart>

Upvotes: 4

Related Questions