为什么我的表格组件无法呈现由传入的渲染函数处理的值?

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

Why can't my table component render the value processed by the render function passed in?

问题

在Vue3中,我想动态渲染我的表格,但没有生效

我定义了一个表格组件

<script setup>
const props = defineProps({
    columns: {
        type: Array,
        default: () => [],
        required: true,
    },
    data: {
        type: Array,
        default: () => [],
        required: true
    }
})
</script>
<template>
    <table class="table__container">
        <thead>
            <tr>
                <th v-for="column in columns" :key="column.key">{{ column.title }}</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="(item, index) in data" :key="index">
                <td v-for="column in columns" :key="column.key">
                    <span v-if="column.render">
                        {{ column.render(item) }}
                    </span>
                    <span v-else>{{ item[column.key] }}</span>
                </td>
            </tr>
        </tbody>
    </table>
</template>

我将columns传递给了表格组件,

const columns = [
    {
        title: '歌曲',
        key: 'name'
    },
    {
        title: '歌手',
        key: 'singer'
    },
    {
        title: '专辑',
        key: 'album'
    },
    {
        title: '时长',
        key: 'duration',
        render: (row) => {
            const { duration } = row;
            return h(
                'span',
                {},
                formatDuration(duration)
            );
        }
    }
]

但是v-if="column.render"没有生效,并且控制台报错 Uncaught (in promise) TypeError: Converting circular structure to JSON --> starting at object with constructor 'Object' | property 'vnode' -> object with constructor 'Object' --- property 'component' closes the circle at JSON.stringify ()。我该如何修复它?

英文:

In Vue3 , I want to dynamically render my table , but did not take effect

I defined a table component

&lt;script setup&gt;
const props = defineProps({
    columns: {
        type: Array,
        default: () =&gt; [],
        required: true,
    },
    data: {
        type: Array,
        default: () =&gt; [],
        required: true
    }
})
&lt;/script&gt;
&lt;template&gt;
    &lt;table class=&quot;table__container&quot;&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th v-for=&quot;column in columns&quot; :key=&quot;column.key&quot;&gt;{{ column.title }}&lt;/th&gt;
            &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
            &lt;tr v-for=&quot;(item, index) in data&quot; :key=&quot;index&quot;&gt;
                &lt;td v-for=&quot;column in columns&quot; :key=&quot;column.key&quot;&gt;
                    &lt;span  v-if=&quot;column.render&quot;&gt;
                        {{  column.render(item) }}
                    &lt;/span&gt;
                    &lt;span v-else&gt;{{ item[column.key] }}&lt;/span&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;
&lt;/template&gt;

I passed columns to the table component ,

const columns = [
    {
        title: &#39;song&#39;,
        key: &#39;name&#39;
    },
    {
        title: &#39;singer&#39;,
        key: &#39;singer&#39;
    },
    {
        title: &#39;album&#39;,
        key: &#39;album&#39;
    },
    {
        title: &#39;duration&#39;,
        key: &#39;duration&#39;,
        render:(row) =&gt; {
            const { duration } = row;
            return h(
                &#39;span&#39;,
                {},
                formatDuration(duration)
            );
        }
    }
]

but v-if = 'column.render' doesn't take effect, and console error
Uncaught (in promise) TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
| property 'vnode' -> object with constructor 'Object'
--- property 'component' closes the circle
at JSON.stringify (<anonymous>)

How can I fix it?

答案1

得分: 1

你可以尝试使用 <component>

<span v-if="column.render">
   <component :is="column.render(item)"></component>
</span>
英文:

You can try using &lt;component&gt;

&lt;span v-if=&quot;column.render&quot;&gt;
   &lt;component :is=&quot;column.render(item)&quot;&gt;&lt;/component&gt;
&lt;/span&gt;

huangapple
  • 本文由 发表于 2023年6月5日 10:19:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403182.html
匿名

发表评论

匿名网友

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

确定