简体   繁体   English

如何渲染一个以 TR 为根元素的 vue 组件?

[英]How to render a vue component with TR as the root element?

I don't have much experience with Vue and facing an issue with redering a component with root as TR element.我对 Vue 没有太多经验,并且面临重新渲染以根作为 TR 元素的组件的问题。

I read this in the docs https://v2.vuejs.org/v2/guide/components.html#DOM-Template-Parsing-Caveats and tried adding the component using is property, but that didn't help either.我在文档https://v2.vuejs.org/v2/guide/components.html#DOM-Template-Parsing-Caveats中阅读了此内容,并尝试使用is属性添加组件,但这也无济于事。 Check the code below.检查下面的代码。

Vue.component('car',{
  props: ['number']
})

new Vue({
  el: "#app"
})
<div id="app">
  <table>
    <tr is='car' inline-template number='123'>
      <tr>
         <td>car no</td>
         <td>{{number}}</td>
      </tr>
    </tr>
    
    <tr is='car' inline-template number='456'>
      <tr>
         <td>car no</td>
         <td>{{number}}</td>
      </tr>
    </tr>
    
  </table>
</div>


This errors out: Error compiling template: Inline-template components must have exactly one child element.这会出错: 编译模板时出错:内联模板组件必须只有一个子元素。

How can I fix this?我怎样才能解决这个问题? Appreciate the help.感谢帮助。

Fiddle https://jsfiddle.net/8d65gvua/小提琴https://jsfiddle.net/8d65gvua/

Update ---- More findings更新----更多发现

If i wrap my tr in a template it works如果我将我的 tr 包装在模板中,它就可以工作

<tr is='car' inline-template number='456'>
  <template>
  <tr>
    <td>car no</td>
    <td>{{number}}</td>
  </tr>
  <template>
</tr>

I have no clue why.我不知道为什么。 Shouldn't the issue with tr be fixed with using the is property? tr 的问题不应该通过使用is属性来解决吗? Why do we need to do this?为什么我们需要这样做?

I've never used inline-template before, my guess is that you are using it to pass the number to your "inner template"?我以前从未使用过inline-template ,我的猜测是您正在使用它将数字传递给您的“内部模板”? You can achieve the same thing by using slots.您可以通过使用插槽来实现相同的目的。 ( Reference ) 参考

I did a test but using slots instead, which I think it's a better way to cater for passing "inner templates".我做了一个测试,但使用了插槽,我认为这是一种更好的方式来满足传递“内部模板”的需求。 Also given the below warning in the documentation with regards to inline-template I would opt for slots.还给出了文档中关于inline-template的以下警告,我会选择插槽。

However, inline-template makes the scope of your templates harder to reason about.但是, inline-template使您的模板的 scope 更难推理。 As a best practice, prefer defining templates inside the component using the template option or in a <template> element in a .vue file.作为最佳实践,最好使用template option在组件内定义模板,或者在.vue文件中的<template>元素中定义模板。

Example例子

Vue.component('my-row', {
    template: `
        <tr>
          <slot></slot>
        </tr>
    `
})

The above is a component to define a single table row.以上是定义单个表格行的组件。 The <slot> element is basically a placeholder for any content you want to pass into this template. <slot>元素基本上是您想要传递到此模板的任何内容的占位符。

Usage用法

<div id="app">
  <table>
    <tr
      is="my-row"
      v-for="todo in todos"
    >
      <td>{{ todo.text }}</td>
    </tr>
  </table>
</div>

What will happen here is that <td>{{ todo.text}}</td> will be placed instead of the <slot> element in the my-row component.这里会发生的是<td>{{ todo.text}}</td>将被放置而不是my-row组件中的<slot>元素。 You can have multiple <td> elements, whatever content you put in the my-row will appear instead of <slot> .你可以有多个<td>元素,无论你放在my-row中的任何内容都会出现而不是<slot>

JS Fiddle: https://jsfiddle.net/x793nub6 JS小提琴: https://jsfiddle.net/x793nub6

PS: Slots! PS:老虎机!

I don't want to make the answer too long, but keep in mind that with slots you can have a super customisable components, of course this all depends on your needs.我不想回答太长,但请记住,使用插槽,您可以拥有超级可定制的组件,当然这一切都取决于您的需求。 But I would suggest having a look at the documentation and trying out a few expirements.但我建议查看文档并尝试一些过期时间。


Update - If you still want to use inline-template更新 - 如果您仍想使用inline-template

I went through the JS Fiddle you shared once more and realised what's the issue you are having.我再次浏览了您分享的 JS Fiddle,并意识到您遇到的问题是什么。 Basically vue is saying that one child needs to be passed.基本上vue是说需要传递一个孩子。 I guess there's some mixup since there's a <tr> nested in another <tr> .我想有一些混淆,因为有一个<tr>嵌套在另一个<tr>中。 To fix it wrap the inner <tr> with a <template> element.要修复它,用<template>元素包裹内部<tr>

From:从:

<tr is='car' inline-template number='123'>
  <tr>
    <td>car no</td>
    <td>{{number}}</td>
  </tr>
</tr>

To:至:

<tr is='car' inline-template number='456'
  <template>
    <tr>
      <td>car no</td>
      <td>{{number}}</td>
    </tr>
  </template>
</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM