gpt4 book ai didi

javascript - Vue 组件 - 在错误的位置呈现

转载 作者:数据小太阳 更新时间:2023-10-29 06:04:53 26 4
gpt4 key购买 nike

我在 vue 组件和内联模板之间看到奇怪的行为。

示例#1:内联模板

这按预期工作。根据下面的示例 #2,vue 组件没有变化,唯一的区别是我将模板内容作为内联模板复制到标签中。

参见:https://jsfiddle.net/pobffmnv/

<div class="panel panel-primary">
<div class="panel-heading">Current Clients</div>
<table class="table table-hover">
<thead>
<tr>
<th>Client Name</th>
<th style="text-align: center;">No. Projects</th>
<th style="text-align: center;">Time (7 days)</th>
<th style="text-align: center;">Edit</th>
</tr>
</thead>
<tbody>
<clientlistitem inline-template>
<tr>
<td>Test</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">
<div class="btn-group btn-group-xs" role="group">
<button type="button" class="btn small btn-primary">Edit</button>
</div>
</td>
</tr>
</clientlistitem>
</tbody>
</table>
</div>

这正确显示了以下内容: Correct

示例#2:非内联

以下是我的 Vue 模板。以下是删除内联模板的代码更改。

参见:https://jsfiddle.net/Ld47hoy2/

<template>
<tr>
<td>Test</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">
<div class="btn-group btn-group-xs" role="group">
<button type="button" class="btn small btn-primary">
Edit
</button>
</div>
</td>
</tr>
</template>
<script>
export default {

}
</script>

更新页面代码:

<div class="panel panel-primary">
<div class="panel-heading">Current Clients</div>
<table class="table table-hover">
<thead>
<tr>
<th>Client Name</th>
<th style="text-align: center;">No. Projects</th>
<th style="text-align: center;">Time (7 days)</th>
<th style="text-align: center;">Edit</th>
</tr>
</thead>
<tbody>
<clientlistitem></clientlistitem>
</tbody>
</table>
</div>

但是,有了上面的内容,显示如下: Incorrect

查看页面输出的源代码,似乎已将呈现的代码放在我的表格之前...?我希望这在 <tbody></tbody> 之间标签..

Strange output

为什么 Vue 会将代码输出到那个位置而不是预期的位置?

最佳答案

这里的问题是 in DOM templates模板首先由浏览器呈现,HTML 表格只允许在其中包含某些类型的元素。在这种情况下,组件 clientlistitem 首先呈现在表的外部,然后 Vue 编译导致组件位于错误位置的模板。

要解决这个问题,请使用 is special attribute .

<tbody>
<tr is="clientlistitem"></tr>
</tbody>

浏览器将接受它并在正确的位置呈现 tr,但 Vue 会将其解释为您的组件。

这是你的 fiddle updated .

关于javascript - Vue 组件 - 在错误的位置呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46374317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com