Tablerow with Vue3 Composition API without Vue.cli not working

huangapple go评论53阅读模式
英文:

Tablerow with Vue3 Composition API without Vue.cli not working

问题

问题与此问题相似:https://stackoverflow.com/questions/38760742/vue-js-table-component-not-working。

不同之处在于,我使用Vue3,使用组合式 API,不使用Vue.cli,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <table>
            <my-tr v-for="(char, i) in chars" :key="i" :char="char" />
        </table>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@3.2"></script>
    <script>
        const myTr = Vue.defineComponent({
            props: ['char'],
            template: '<tr><td>{{char}}</td></tr>'
        });
        Vue.createApp({
            setup() {
                return {
                    chars: Vue.ref(['a', 'b', 'c'])
                };
            }
        }).component('my-tr', myTr).mount('#app');
    </script>
</body>
</html>

表格没有正确渲染,但不会在控制台中引发错误:

<div id="app" data-v-app="">
    <tr>
        <td>a</td>
    </tr>
    <tr>
        <td>b</td>
    </tr>
    <tr>
        <td>c</td>
    </tr>
    <table></table>
</div>

<table>替换为<div>可以解决此问题,但对我来说替换表格不是一个选项。

我做错了什么?

英文:

My problem is similar to this question: https://stackoverflow.com/questions/38760742/vue-js-table-component-not-working.

The difference is that I use Vue3, with the Composition API and without Vue.cli as follows:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div id=&quot;app&quot;&gt;
        &lt;table&gt;
            &lt;my-tr v-for=&quot;(char, i) in chars&quot; :key=&quot;i&quot; :char=&quot;char&quot; /&gt;
        &lt;/table&gt;
    &lt;/div&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/vue@3.2&quot;&gt;&lt;/script&gt;
    &lt;script&gt;
        const myTr = Vue.defineComponent({
            props: [&#39;char&#39;],
            template: &#39;&lt;tr&gt;&lt;td&gt;{{char}}&lt;/td&gt;&lt;/tr&gt;&#39;
        });
        Vue.createApp({
            setup() {
                return {
                    chars: Vue.ref([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;])
                };
            }
        }).component(&#39;my-tr&#39;, myTr).mount(&#39;#app&#39;);
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

The table is not rendered correctly - without causing an error on the console:

&lt;div id=&quot;app&quot; data-v-app=&quot;&quot;&gt;
    &lt;tr&gt;
        &lt;td&gt;a&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;b&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;c&lt;/td&gt;
    &lt;/tr&gt;
    &lt;table&gt;&lt;/table&gt;
&lt;/div&gt;

Replacing &lt;table&gt; with &lt;div&gt; fixed it - but replacing the table is not an option for me.

What am I doing wrong?

答案1

得分: 1

以下是翻译好的部分:

You are running into a restriction of HTML, where <table> allows only for specific tags as children (tr, thead, tbody, tfoot). This applies to your custom <my-tr> tag. The documentation recommends to use :is as a workaround:

<tr is="vue:my-tr" v-for="(char, i) in chars" :key="i" :char="char"></tr>
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
const myTr = Vue.defineComponent({
  props: ['char'],
  template: '<tr><td>{{char}}</td></tr>'
});

Vue.createApp({
  setup() {
    return {
      chars: Vue.ref(['a', 'b', 'c'])
    };
  }
}).component('my-tr', myTr).mount('#app');

<!-- language: lang-css -->
table{
  border: 1px solid grey;
}

<!-- language: lang-html -->
<div id="app">
  <table>
    <tr is="vue:my-tr" v-for="(char, i) in chars" :key="i" :char="char"></tr>
  </table>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@3.2"></script>

<!-- end snippet -->
英文:

You are running into a restriction of HTML, where &lt;table&gt; allows only for specific tags as children (tr, thead, tbody, tfoot). This applies to your custom &lt;my-tr&gt; tag. The documentation recommends to use :is as a workaround:

&lt;tr is=&quot;vue:my-tr&quot; v-for=&quot;(char, i) in chars&quot; :key=&quot;i&quot; :char=&quot;char&quot;&gt;&lt;/tr&gt;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const myTr = Vue.defineComponent({
  props: [&#39;char&#39;],
  template: &#39;&lt;tr&gt;&lt;td&gt;{{char}}&lt;/td&gt;&lt;/tr&gt;&#39;
});

Vue.createApp({
  setup() {
    return {
      chars: Vue.ref([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;])
    };
  }
}).component(&#39;my-tr&#39;, myTr).mount(&#39;#app&#39;);

<!-- language: lang-css -->

table{
  border: 1px solid grey;
}

<!-- language: lang-html -->

&lt;div id=&quot;app&quot;&gt;
  &lt;table&gt;
    &lt;tr is=&quot;vue:my-tr&quot; v-for=&quot;(char, i) in chars&quot; :key=&quot;i&quot; :char=&quot;char&quot;&gt;&lt;/tr&gt;
  &lt;/table&gt;
&lt;/div&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vue@3.2&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月3日 20:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定