Vue + Vuetify 数据表第一列图片

Dil*_*ill 4 vue.js vuejs2 vuetify.js vue-data-tables

我正在使用Vue + Vuetify,我正在尝试在第一列中添加图像。

表格模板代码

<v-data-table
  :headers="headers"
  :items="desserts"
  :search="search"
  light
>
  <template slot="items" slot-scope="props">
    <td><img :src="props.item.name" style="width: 50px; height: 50px"></td>
    <td class="text-xs-right">{{ props.item.calories }}</td>
    <td class="text-xs-right">{{ props.item.fat }}</td>
    <td class="text-xs-right">{{ props.item.carbs }}</td>
    <td class="text-xs-right">{{ props.item.protein }}</td>
    <td class="text-xs-right">{{ props.item.iron }}</td>
  </template>
  <v-alert slot="no-results" :value="true" dir="rtl" color="error" icon="warning">
    {{ PRODUCTS_TABLE_TEXT.NO_RESULT }} "{{ search }}"
  </v-alert>
</v-data-table>
Run Code Online (Sandbox Code Playgroud)

表脚本代码

data () {
  return {
    //TEXT
    PRODUCTS_TABLE_TEXT: products_table_text,
    //TABLE DATA
    search: '',
    headers: [
      {
        text: products_table_text.columns.IMAGE,
        align: 'center',
        sortable: false,
        value: 'image'
      },
      { text: 'Calories', value: 'calories' },
      { text: 'Fat (g)', value: 'fat' },
      { text: 'Carbs (g)', value: 'carbs' },
      { text: 'Protein (g)', value: 'protein' },
      { text: 'Iron (%)', value: 'iron' }
    ],
    desserts: [
      {
        value: false,
        name: '1.jpg',
        calories: 159,
        fat: 6.0,
        carbs: 24,
        protein: 4.0,
        iron: '1%'
      },
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我试图做的

我尝试在name变量中添加HTML 图像代码,如下所示:

name: '<img src="./../../assets/products-images/1.jpg" style="width: 50px; height: 50px">'
Run Code Online (Sandbox Code Playgroud)

但它只是将 HTML 打印为文本,并没有呈现它。

小智 9

我也被堆积了一分钟,但这是在 vuetify 数据表表格模板代码中使用图像的简单方法

  <template>
 <v-layout row wrap>
      <v-flex xs12>
        <v-data-table :headers="headers" :items="carts" class="elevation-0">
          <template v-slot:item.image="{ item }">
            <div class="p-2">
              <v-img :src="item.image" :alt="item.name" height="100px"></v-img>
            </div>
          </template>
        </v-data-table>
      </v-flex>
    </v-layout>
<template/>
Run Code Online (Sandbox Code Playgroud)

表脚本代码

  <script>
import { mapGetters } from "vuex";

export default {
    data() {
    return {
      user: null,
      isAvalibel: false,
      headers: [
        { text: "Image", value: "image", sortable: false },
        { text: "Product Name", value: "name", sortable: false },
      ]
    };
 computed: {
    ...mapGetters(["carts"])
  },
Run Code Online (Sandbox Code Playgroud)


Sag*_*mal 1

在模板编译过程中,编译器可以将某些属性(例如 src URL)转换为 require 调用,以便 webpack 可以处理目标资源。例如,<img src="./foo.png">将尝试在文件系统上找到该文件./foo.png并将其作为包的依赖项包含在内。

所以,对于动态属性 src,

<td> <img :src="require('./assets/products-images/' +props.item.name)"> </td>