Home  >  Q&A  >  body text

"How to insert pictures into the table header in vue.js 3"

<p>I am using v-data-table from vue.js 3 and want to insert a picture in the table header. So I tried overriding the table header template: </p> <pre class="lang-html prettyprint-override"><code><template v-for="header in headers" v-slot:[`header.${header.value}`]=" ;{ headers }"> {{ header.text }} </template> </code></pre> <p>This method is effective. However, if I include the image like this, I get an error: </p> <pre class="lang-html prettyprint-override"><code><template v-for="header in headers" v-slot:[`header.${header.value}`]=" ;{ headers }"> {{ header.text }} <span v-if="header.text=='SomeText'"><img :src="require('@/assets/image_1.png')" /></span> ; <span v-if="header.text=='SomeOtherText'"><img :src="require('@/assets/image_2.png')" /></span> ; </template> </code></pre> <p>The error message is as follows:</p> <pre class="brush:php;toolbar:false;">14:3 error Elements in iteration expect to have 'v-bind:key' directives vue/require-v-for-key</pre> <p>I would be happy if anyone could offer advice. </p>
P粉550257856P粉550257856392 days ago477

reply all(1)I'll reply

  • P粉127901279

    P粉1279012792023-08-28 00:16:35

    Attempt to type a nested element based on the index of the current heading:

    <template v-for="(header,i) in headers" v-slot:[`header.${header.value}`]="{ headers }">
        <span :key="'text'+i"> {{ header.text }}</span>
        <span :key="'img1'+i" v-if="header.text=='SomeText'"><img :src="require('@/assets/image_1.png')" /></span>
        <span :key="'img2'+i" v-if="header.text=='SomeOtherText'"><img :src="require('@/assets/image_2.png')" /></span>
    </template>

    reply
    0
  • Cancelreply