简体   繁体   English

无法在Vuejs中显示图像

[英]Cannot display images in Vuejs

I've set up a carousel using bootstrap-vue. 我使用bootstrap-vue设置了一个轮播。 The carousel-items are created dynamically using a for loop and an array. carousel-items是使用for循环和数组动态创建的。 The array contains three objects with the following keys: id, caption, text and image path. 该数组包含三个具有以下键的对象:id,caption,text和image path。 So far, I am able to display the caption and text but not the images. 到目前为止,我能够显示标题和文字, 但不能显示图像。 I checked the console and there was no error. 我检查了控制台,没有错误。 Hence, I am very confused. 因此,我很困惑。 Below are my codes and screenshots of the problem 以下是我的问题代码和截图

UPDATE: I am trying to fix this problem for 2 days already, help is greatly needed! 更新:我已经尝试解决这个问题2天了,非常需要帮助!

This is my project structure 这是我的项目结构

项目结构

This are my codes 这是我的代码

<template>
  <div>
    <b-carousel
      id="carousel-1"
      v-model="slide"
      :interval="10000"
      controls
      indicators
      background="#ababab"
      style="text-shadow: 1px 1px 2px #333;"
      @sliding-start="onSlideStart"
      @sliding-end="onSlideEnd"
    >
      <b-carousel-slide
        v-for="item in carouselItems"
        :key="item.id"
        :caption="item.caption"
        :text="item.text"
        :style="{ 'background-image': 'url(' + item.image + ')' }"
      ></b-carousel-slide>
    </b-carousel>
  </div>
</template>

<script>

export default {
  data() {
    return {
      carouselItems: [
        {
          id: 1,
          caption: "Memories",
          image: "@/assets/images/Homepage-min.jpg",
          text: "Memories(1)"
        },
        {
          id: 2,
          caption: "Memories",
          image: "@/assets/images/Homepage-min.jpg",
          text: "Memories(2)"
        },
        {
          id: 3,
          caption: "Memories",
          image: "@/assets/images/Homepage-min.jpg",
          text: "Memories(3)"
        }
      ],
      slide: 0,
      sliding: null
    };
  },
  methods: {
    onSlideStart(slide) {
      this.sliding = true;
    },
    onSlideEnd(slide) {
      this.sliding = false;
    }      
  }
};
</script>

Here is a screenshot: 这是一个截图: 图像未显示

Update I went to take a look at the networks tab to see if I specified the wrong image path. 更新我去看看网络选项卡,看看我是否指定了错误的图像路径。 However, the image path was correct as the status code is 304. What seems to be the problem? 但是,图像路径是正确的,因为状态代码是304.这似乎是什么问题? Can anyone help? 有人可以帮忙吗?

在此输入图像描述

Update: I've removed the style and replaced it with the following code 更新:我删除了样式并将其替换为以下代码

:img-src="item.image"

After I save the changes and refresh, I can see an icon indicating that the browser could not load the images. 保存更改并刷新后,我可以看到一个图标,表明浏览器无法加载图像。

在此输入图像描述

Update I've successfully rendered the images to the carousel-slide. 更新我已成功将图像渲染到旋转木马幻灯片。 Apparently, "require" is needed if the image path is relative. 显然,如果图像路径是相对的,则需要“需要”。 I've updated this portion of my code to the following 我已将此部分代码更新为以下内容

data() {
    return {
      carouselItems: [
        {
          id: 1,
          caption: "Memories",
          image: require("@/assets/images/Homepage-min.jpg"),
          text: "Memories(1)"
        },
        {
          id: 2,
          caption: "Memories",
          image: require("@/assets/images/Homepage-min.jpg"),
          text: "Memories(2)"
        },
        {
          id: 3,
          caption: "Memories",
          image: require("@/assets/images/Homepage-min.jpg"),
          text: "Memories(3)"
        }
      ],
      slide: 0,
      sliding: null
    };
  },

Update However, now I face another problem, the image does not fit the entire height of the screen when in mobile mode. 更新但是,现在我面临另一个问题,在移动模式下,图像不适合屏幕的整个高度。 I want it to be responsive. 我希望它能够做出回应。 Here is a screen shot of my problem 这是我的问题的屏幕截图

Update 7/5/2019 I am still trying to fix this issue, can anyone help me with the CSS? 更新 7/5/2019我仍然在尝试解决这个问题,任何人都可以帮我解决这个问题吗?

图像无响应,不适合移动设备中的屏幕

Try using img-src="https://xxxxx" instead of changing the style . 尝试使用img-src="https://xxxxx"而不是更改style You should get something like that in your code: 你应该在你的代码中得到类似的东西:

     <b-carousel-slide
        v-for="item in carouselItems"
        :key="item.id"
        :caption="item.caption"
        :text="item.text"
        :img-src=item.image
      ></b-carousel-slide>

Or you can just use an actual img tag inside the <b-carousel-slide> : 或者您可以在<b-carousel-slide>使用实际的img标签:

     <b-carousel-slide>
        <img
          slot="img"
          class="d-block img-fluid w-100"
          width="1024"
          height="480"
          src="https://xxxxxx"
          alt="image slot"
        >
      </b-carousel-slide>

You should also check your pictures 'path' since webpack adds a unique-id automatically to each image and can sometimes be troublesome in certain cases, just check that the webpack config is working on your images. 您还应该检查图片的“路径”,因为webpack会自动为每个图像添加一个唯一ID,在某些情况下有时会很麻烦,只需检查webpack配置是否正在处理您的图像。

<b-carousel>不会裁剪图像,它会尝试保留其原始宽高比(与本机Bootstrap V4.x轮播相同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM