【发布时间】:2017-01-20 16:38:38
【问题描述】:
我正在尝试将自定义输入创建为 VueJS 组件。它将具有<input type="text"> 字段和按钮。该组件必须实现这样的行为:您可以键入带有自动完成功能的文本或按下按钮打开带有数据库记录的模式对话框,然后选择一个。像这样的:
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for...">
<span class="input-group-btn">
<button class="btn btn-default" type="button" @click="openModal">Choose</button>
</span>
</div>
模态对话框将包含复杂的逻辑和大量的 HTML 代码。我想我会把模态对话框放在其他组件中。
毕竟我的自定义输入组件将在页面上的表格行中使用,例如:
<tr v-for="item in items">
<td><input-component :item="item"><input-component></td>
</tr>
该表可能包含 10-30 行,这是一个问题 - 我应该从我的自定义输入组件中排除繁重的模式对话框代码,还是在 VueJS 中可以在 DOM 中有如此多的数十个重复项?
我应该选择什么变体:
1) 排除模态对话框,将其放在页面顶部一次,然后从自定义输入组件中调用它
<body>
<modal-component></modal-component>
<table><tbody>
<tr v-for="item in items">
<td><input-component :item="item"><input-component></td>
</tr>
</tbody></table>
</body>
2) 包含模态对话框并在 DOM 中有数十个重复代码
<body>
<table><tbody>
<tr v-for="item in items">
<td><input-component :item="item"><input-component></td><!--now contains <modal-component></modal-component>-->
</tr>
</tbody></table>
</body>
【问题讨论】: