javascript - 无法使用 VueJS 通过组件渲染表格行
问题描述
我正在尝试使用组件来使用 VueJS 呈现表格行,但是这样做时我无法读取对象的属性。我有两个组件:ProspectsIndex 和 Player。
这是 ProspectsIndex 模板:
<template>
<table class="table table-hover">
<thead>
<tr>
<th>Player</th>
</tr>
</thead>
<tbody>
<tr
v-for="(player, index) in prospects"
v-bind:player="player"
v-bind:index="index"
v-bind:key="player.personId"
is="player"
>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import Player from './Player';
export default {
name: 'ProspectsIndex',
components: {
Player
},
data() {
return {
position: '-',
status: '-',
name: '',
schools: this.$parent.$parent.schools,
prospects: this.$parent.$parent.prospects
};
}
}
</script>
在我尝试将行拆分为自己的组件之前,这工作得很好。我这样做是为了在计算属性和其他事情、缩放、关注点分离等方面提供一些帮助。这是 Player 组件:
<template>
<tr>
<td valign="top" width="5%" style="padding:0">
{{player.lastName}}
</td>
</tr>
</template>
<script>
export default {
name: 'Player'
}
</script>
我尝试了多种不同的方法将播放器对象放入播放器组件中,但均无济于事。下面是播放器对象的样子:
{
"personId":2559901,
"firstName":"SAQUON",
"lastName":"BARKLEY",
"pickAnalysis":null,
"hasAnalysis":false,
"video":null,
"pick":null,
"college":38,
"pos":"RB",
"height":"6'0\"",
"weight":233,
"armLength":"31 3/8",
"handSize":"9 1/2",
"expertGrade":7.45,
"fanPick":null,
"schoolYear":"Junior",
}
现在,如果在 Player 组件中我将 {{player.lastName}} 替换为“testing”,我会看到打印了数百行测试。所以这部分工作,它只是访问让我难过的播放器对象!
解决方案
在您的Player
组件中,添加一个prop
以接受player
来自父范围的对象,即
export default {
name: 'Player',
props: {
player: Object
}
}
你已经v-bind:player="player"
在你的父范围内,所以这应该是你所需要的。
推荐阅读
- javascript - 将项目放入网格和相邻框中的更干净的方法?
- batch-file - 我想在没有 Windows 任务计划程序的情况下计划批处理文件
- c - 求 2 名学生的平均值
- qt - 当我的应用程序未在 qt 中运行时如何通知用户?
- excel - vba:复制工作表内容(不是工作表)
- android - 包含合并标签的 ConstraintLayout 不起作用
- java - Spring Data JPA 原生 SQL 查询 DELETE SQL 查询
- ruby-on-rails - 如何使用 rvm 将 jemalloc 添加到现有的 Rails 服务器?
- azure - 如何将现有 Azure Functions 移动/集成到源代码控制?
- ruby-on-rails - Rails kaminari:如何为分页指定 total_count?