【发布时间】:2017-12-11 02:23:20
【问题描述】:
我正在尝试通过 Vue.js 2 中的相对路径加载我的图像,但似乎有些不对劲。我只是对 Vue 感到困惑,并希望得到任何指点。所述图像位于 /src/assets/imgs 目录中。
以下是相关组件的相关代码 sn-ps。
<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”文件夹中。
【问题讨论】:
-
检查
src分配给图像的内容,您的代码工作正常,可能路径需要一些调整。 jsfiddle.net/s379dL6s -
@yuriy636 我得到了“正确”的路径,正如在简单的 .html 文件中引用它们时所预期的那样,但图像不会加载。我不知道他们为什么不这样做。
-
你能张贴你的文件树的截图吗?
-
@aprouja1 我已经包含了我的项目文件树的简单屏幕截图。
-
如果你正在使用来自
vue-cli的 webpack 模板(看起来像你),那么请阅读这篇文章~vuejs-templates.github.io/webpack/static.html
标签: javascript vue.js vuejs2 vue-component