如何检索用于VUE文件中的图像?
我一直在创建一个投资组合网站,通常在使用NUXT时,我有一个资产文件夹,这次我没有一个。
因此,我手动创建了一个并将图像添加到其中,但是,我无法检索用于VUE文件上的图像。
但是,我在下面收到错误:
ERROR Failed to compile with 1 errors
This dependency was not found:
* ~/assets/ethereum.jpg in ./node_modules/babel-loader/lib??ref--2-0!./node_modules/vue-loader/lib??vue-loader-options!./pages/previousWork.vue?vue&type=script&lang=js&
To install it, you can run: npm install --save ~/assets/ethereum.jpg
以下是我的“以前的work.vue”文件中找到的代码。
<template>
<div class="bg-black h-screen px-6 py-12">
<h2 class="text-white font-exo text-5xl ">
PREVIOUS WORK
</h2>
<div class="flex space-x-2">
<div v-for="project in projects">
<img :src="project.image.url" alt="" style="height: 70vh;" class="object-cover">
<p class="font-space-mono text-white text-sm">{{project.title}}</p>
</div>
</div>
</div>
</template>
<script>
import ethereumImg from '~/assets/ethereum.jpg'
export default {
data() {
return {
projects: [
{
image: {
url: ethereumImg
},
title: 'ETHEREUM CARBON CALCULATOR'
}
]
}
}
}
</script>
以下是我的“ package.json”文件中找到的代码。 (依赖项等)
{
"name": "my-portfolio-nuxt",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start",
"generate": "nuxt generate"
},
"dependencies": {
"core-js": "^3.19.3",
"dat.gui": "0.7.7",
"gsap": "3.6.0",
"nuxt": "^2.15.8",
"orbit-controls-es6": "^2.0.1",
"three": "^0.126.1",
"vue": "^2.6.14",
"vue-server-renderer": "^2.6.14",
"vue-template-compiler": "^2.6.14",
"webpack": "^4.46.0"
},
"devDependencies": {
"@nuxtjs/google-fonts": "^1.3.0",
"@nuxtjs/tailwindcss": "^4.2.1",
"postcss": "^8.4.4"
}
}
以下是用于此站点的文件结构。 使用中的文件结构
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您不必将图像导入
/Assets
包含您未编译的资产,例如手写笔或SASS文件,图像或字体。在VUE模板内部,如果您需要链接到资产目录〜/Assets/ethereum.jpg
,则使用斜杠。在CSS文件中,如果您需要引用资产目录,请使用
〜Assets/your_image.png
(无斜杠) ,在使用NUXT时,建议将图像文件
/static
。静态目录直接映射到服务器root(),并包含可能不会更改的文件。所有随附的文件将由NUXT自动提供,并可以通过您的项目根网址访问。Firstly you don't have to import the image as
/assets
contains your un-compiled assets such as Stylus or SASS files, images, or fonts. Inside your vue templates, if you need to link to your assets directory use~/assets/ethereum.jpg
with a slash before assets.Inside your css files, if you need to reference your assets directory, use
~assets/your_image.png
(without a slash)As you are using Nuxt I would suggest putting the image files inside
/static
. The static directory is directly mapped to the server root () and contains files that likely won't be changed. All included files will be automatically served by Nuxt and are accessible through your project root URL.如,问题主要是文件名的错字,
Etheruem
而不是以太坊
。解决了问题。As mentioned in this comment by Estus Flask, the issue was mainly a typo in the name of the file,
etheruem
rather thanethereum
. That fixed the issue.