javascript - VueJS - 无法将 Prop 从父组件传递到子组件

标签 javascript vue.js vue-props

这是我的 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>

这是我的子组件:

<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>

我可以使用 Prop 传递数据,但不能传递类型。当我将 type: this.type 更改为 type: "bar" 时,它会按预期工作,因此代码本身没有错误。

我收到以下警告:

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.

我做错了什么?

最佳答案

您正在尝试将 prop“类型”动态绑定(bind)bar,就好像最后一个是变量一样。如果您想将其作为字符串传递,请执行以下操作:

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

关于javascript - VueJS - 无法将 Prop 从父组件传递到子组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63225972/

相关文章:

javascript - 如何在 Angular Js 中找到完全加载的 View

javascript - Vue : How to use prop value inside component template

javascript - 如何隐藏与当前值不匹配的元素并与prop进行比较

laravel - VueJS - 如何将输入参数传递给单页面组件

javascript - 访问 Vuetify 作用域槽内表 colspan 的 number prop 值

javascript - 自 Firefox 49 以来,MJPEG 流在第一帧卡住

javascript - 如何使用Later.js验证cron表达式

javascript - 如何使用 HTTP POST 请求将参数发送到 Iframe

javascript - - 不能在有状态组件根元素上使用 v-for,因为它呈现多个元素

javascript - 拖放后重置所有项目的 Vue.js 列表顺序