简体   繁体   English

如何在Vue中将多个道具从父组件传递给子组件

[英]How to pass multiple props from parent to child component in Vue

I'm trying to pass two properties from parent to child, but for some reason this isn't working and all the examples I've found refer to passing a single property. 我试图将两个属性从父级传递给子级,但由于某种原因,这不起作用,我发现的所有示例都引用了传递单个属性。 What I've tried to do is: 我试图做的是:

Parent vue component: 父vue组件:

<template>
  <div class="statistics_display">
    <multiLineChart :rowsA="reading['A'].price_stats" :rowsB="reading['B'].price_stats"></multiLineChart>
  </div>
</template>

multiLineChart vue component: multiLineChart vue组件:

export default {
  name: 'MultiLineChart',
  props: ['rowsA', 'rowsB'],
  mounted: function() {
      console.log(this.rowsA);
  }

the console log is returning undefined. 控制台日志返回undefined。 If I executethe exact same code and pass a single prop, it returns the expected prop contents. 如果我执行完全相同的代码并传递一个道具,它将返回预期的道具内容。 What am I missing? 我错过了什么?

HTML attributes are case-insensitive, so HTML属性不区分大小写,所以

<multiLineChart :rowsA="reading['A'].price_stats" :rowsB="reading['B'].price_stats"></multiLineChart>

Are actually bound to props: ['rowsa', 'rowsb'] . 实际上是绑定props: ['rowsa', 'rowsb']

If you want props: ['rowsA', 'rowsB'] to work, use, in the template: :rows-a="..." and :rows-b="..." . 如果你想要props: ['rowsA', 'rowsB']工作,请在模板中使用:rows-a="...":rows-b="..."

See it working below. 看下面的工作。

 Vue.component('multilinechart', { template: "#mtemplate", props: ['rowsA', 'rowsB'], mounted: function() { console.log(this.rowsA, this.rowsB); } }) new Vue({ el: '#app', data: { reading: {A: {price_stats: 11}, B: {price_stats: 22}} } }); 
 <script src="https://unpkg.com/vue@2.5.13/dist/vue.min.js"></script> <div id="app"> <div class="statistics_display"> <multiLineChart :rows-a="reading['A'].price_stats" :rows-b="reading['B'].price_stats"></multiLineChart> </div> </div> <template id="mtemplate"> <div>I'm multilinechart</div> </template> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM