如何检索用于VUE文件中的图像?

发布于 2025-01-31 04:43:15 字数 2235 浏览 0 评论 0 原文

我一直在创建一个投资组合网站,通常在使用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"
  }
}

以下是用于此站点的文件结构。 使用中的文件结构

I have been making a portfolio website, usually when using Nuxt I have an assets folder, this time I did not have one.

Therefore, I created one manually and added my images into it, however, I cannot retrieve the images for use on my vue file.

However, I get the error below:

 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  

Below is the code found in my "previousWork.vue" file.

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

Below is the code found in my "package.json" file. (dependencies etc)

{
  "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"
  }
}

Below is the file structure used for this site.
File Structure in use

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫尐诺 2025-02-07 04:43:15

首先,您不必将图像导入/Assets 包含您未编译的资产,例如手写笔或SASS文件,图像或字体。在VUE模板内部,如果您需要链接到资产目录〜/Assets/ethereum.jpg ,则使用斜杠。

<template>
  <img src="~/assets/ethereum.jpg" />
</template>

在CSS文件中,如果您需要引用资产目录,请使用 〜Assets/your_image.png (无斜杠)

background: url('~assets/ethereum.jpg');

在使用NUXT时,建议将图像文件/static 。静态目录直接映射到服务器root(),并包含可能不会更改的文件。所有随附的文件将由NUXT自动提供,并可以通过您的项目根网址访问。

<!-- Static image from static directory -->
<img src="/ethereum.jpg" />

<!-- webpacked image from assets directory -->
<img src="~/assets/ethereum.jpg" />

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.

<template>
  <img src="~/assets/ethereum.jpg" />
</template>

Inside your css files, if you need to reference your assets directory, use ~assets/your_image.png (without a slash)

background: url('~assets/ethereum.jpg');

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.

<!-- Static image from static directory -->
<img src="/ethereum.jpg" />

<!-- webpacked image from assets directory -->
<img src="~/assets/ethereum.jpg" />
千笙结 2025-02-07 04:43:15

As mentioned in this comment by Estus Flask, the issue was mainly a typo in the name of the file, etheruem rather than ethereum. That fixed the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文