为什么我的图像不能加载到Vue.js 2中? [英] Why don't my images load in Vue.js 2?

查看:69
本文介绍了为什么我的图像不能加载到Vue.js 2中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图通过Vue.js 2中的相对路径来加载我的图像,但似乎有些东西掉了。我只是对Vue表示不满,并且会欣赏任何指针。所述图像位于/ src / assets / imgs目录中。

I'm trying to get my images to load via a relative path in Vue.js 2 but something seems off. I'm just getting my feet wet with regard to Vue and would appreciate any pointers. Said images are in the /src/assets/imgs directory.

以下是相关组件的相关代码片段。

Below are the relevant code snippets for the component in question.

<template>
  <section class="container sources">
        <h2>{{ heading }}</h2> 
        <div class="columns">
            <div class="column" v-for="source in sources">
                <figure :title="source">
                    <img :src="imgPath + source + '.png'" :alt="source + ' logo'">
                    <figcaption>{{ source }}</figcaption>        
                </figure>        
            </div>
        </div>
    </section>
</template>

<script>
export default {
   name: 'sources',
   data () {
      return {
          heading: 'News Sources',
          imgPath: 'src/assets/imgs/',
          sources: [
            'al-jazeera-english', 'associated-press', 'bbc-news', 'bbc-sport', 'business-insider', 'cnn', 
            'daily-mail', 'entertainment-weekly', 'espn', 'financial-times', 'fox-sports', 'hackernews', 
            'ign','independent', 'mtv-news', 'national-geographic', 'new-scientist', 'reuters', 'techcrunch', 'the-economist', 'the-guardian-uk', 'the-huffington-post', 'the-new-york-times', 
                'the-washington-post'
           ]
      }
   }
}
</script>

编辑:添加了项目(简单)文件树的屏幕截图请求。当然,所述图像位于'imgs'文件夹中。

Added a screenshot of my project's (simple) file tree upon request. Said images are in the 'imgs' folder, of course.

推荐答案

假设您正在使用<$ c中的webpack模板$ c> vue-cli ,你可以简单地使用静态资产

Assuming you're using the webpack template from vue-cli, you can simply use a relative path for static assets.

<img :src="'./assets/imgs/' + source + '.png'"






或者,把你的 static 目录中的图像,并用绝对路径引用它们,即


Alternatively, put your images in the static directory and reference them with an absolute path, ie

<img :src="'/static/imgs/' + source + '.png'"

这篇关于为什么我的图像不能加载到Vue.js 2中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆