V-for显示对象的特定属性

时间:2019-06-13 14:22:36

标签: javascript html vue.js

我要制作很多桌子,所以我做了一个桌子制作组件。每个表只有3种不同的东西:

  1. 对象(对象数组。每个对象1行)
  2. 特定于该表的标题
  3. 我要显示的对象属性

我的组件:

Vue.component('table-component', {
  props: {
    headers: Array,
    data_for_rows: Object,
    data_properties_to_display: Array,
  },
  template: `
  <div>
    <table class="table-hover">
      <thead>
          <tr>
              <th v-for='item in headers'>{{item}}</th>
          </tr>
      </thead>
      <tbody>
          <tr v-for='object in data_for_rows'>
              <td v-for='property in data_properties_to_display'>{{object.property}}</td>
          </tr>
      </tbody>
  </table>
</div>
`
});

HTML本身:

                    <table-component :headers="['Party','Members','Votes With Party %']" data_for_rows="glance_data"
                        :data_properties_to_display="['partyName','memberCount','percentage']">
                    </table-component>

目标是能够打印对象内的属性值(特别是通过将对象本身和要打印的属性作为单独的道具传递给组件)。我可以吗?

1 个答案:

答案 0 :(得分:0)

{{object.property}}会在该嵌套循环的property实例中寻找名为object的属性,对吗?我在想您可能想要更多类似{{object[ property ]}}的东西。