【问题标题】:Vue Component - Rendering in wrong locationVue 组件 - 在错误的位置渲染
【发布时间】:2018-03-04 14:23:45
【问题描述】:

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

示例 #1: 内联模板

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

见: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>

这正确显示了以下内容:

示例#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>

但是,使用上述内容,将显示以下内容:

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

Vue 将代码输出到该位置而不是预期的位置的任何原因?

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component


    【解决方案1】:

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

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

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

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

    这是你的小提琴updated

    【讨论】:

    • 太棒了。所以你是说,不管 DOM 中的任何位置如何,使用“is”总是更安全?
    • @Kannaiyan 不,使用is在非常特殊的情况下需要的,例如表格。最安全的方法是使用字符串模板或单个文件组件。看看答案中的第一个链接。
    猜你喜欢
    • 2017-04-25
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 2020-03-25
    • 2019-04-19
    相关资源
    最近更新 更多