5

我正在使用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>

表脚本代码

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%'
      },
    ]
  }
}

我试图做的

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

name: '<img src="./../../assets/products-images/1.jpg" style="width: 50px; height: 50px">'

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

4

4 回答 4

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/>

表脚本代码

  <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"])
  },

于 2019-11-14T11:30:28.243 回答
1

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

所以,对于动态属性 src,

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

于 2018-08-21T16:21:07.877 回答
0

Actually there are many ways you can use to insert image inside your Vue app/template:

1) (This is what you need for your code) Using require function actually this is going to be handled by the webpack and map the image to it's resource.

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

please follow the below github discussion for more explanation:

https://github.com/vuejs-templates/webpack/issues/126

2) Another way read the image as base64 from the server and handle that inside your Vue app:

<img v-bind:src="'data:image/jpeg;base64,'+imageBytes" />
于 2018-08-22T02:01:25.963 回答
0

如果您想将服务器图像与 axios 一起使用并将它们显示在行中,这也可以。

<template v-slot:item.image="{ item }">
    <img v-bind:src="`/${item.image}`" style="width: 50px; height:50px">
    </v-img>
</template>
于 2021-10-07T13:35:23.790 回答