Display image in Vue

User
displayvue src/assets/images folder image in vue files
Assistant
To display an image from the `src/assets/images` folder in your Vue component, you can use the `@` alias which points to the `src` directory in most Vue CLI or Vite setups.

### Example Structure:

src/
  assets/
    images/
      logo.png
  components/
    MyComponent.vue

### How to Display the Image:

<template>
  <div>
    <img :src="logo" alt="My Logo" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      logo: require('@/assets/images/logo.png')  // for Vue 2 or options API
    };
  }
};
</script>

### Or (in Vue 3 with “ or composition API):

<template>
  <div>
    <img :src="logoUrl" alt="My Logo" />
  </div>
</template>

<script setup>
const logoUrl = new URL('@/assets/images/logo.png', import.meta.url).href;
</script>

### Tips:
– `@` is a webpack alias for `src`, configured in most Vue projects.
– For **Vite**, use the `new URL(…, import.meta.url)` approach (especially in “).
– Make sure your image file is correctly named and located.

Let me know if you're using a specific build tool (Vue CLI, Vite, etc.) and I'll tailor the solution!

User
use views
Assistant
Got it! If you're using **Vue 3 + Vite** (most likely with Laravel Breeze + Inertia, based on your previous setup) and you want to display an image from `src/assets/images` inside a `.vue` file located in your `resources/js/Pages` or `resources/js/Views` directory, here's the right way to do it.

### ✅ Recommended Approach for Vite + Vue 3

Let’s say your folder structure is:

resources/
  js/
    Views/
      Home.vue
  assets/
    images/
      banner.jpg

Then in `Home.vue`:

<template>
  <div>
    <img :src="bannerImage" alt="Banner" />
  </div>
</template>

<script setup>
const bannerImage = new URL('@/assets/images/banner.jpg', import.meta.url).href;
</script>

### ✅ Alternate: Direct in Template (only works for static image paths)

<template>
  <img src="@/assets/images/banner.jpg" alt="Banner" />
</template>

But this might not resolve properly in all Vite setups—especially with dynamic images or if `@` isn't recognized in the template. So prefer the `new URL(…).href` method in “ for reliability.

Let me know if you're using a dynamic image (e.g. from a loop), or want to load images by filename from props — I can help with that too.

User
use directly in image src
Assistant
If you want to **use the image directly in the `src` attribute** inside the “ without declaring it in “, here's how to do it in a **Vue 3 + Vite** project:

### ✅ Use `import.meta.url` with `new URL()` inline:

<template>
  <img :src="new URL('@/assets/images/banner.jpg', import.meta.url).href" alt="Banner" />
</template>

### ❌ This won’t work in Vite:

<!-- This is incorrect in Vite (only works in Webpack/Vue CLI setups) -->
<img src="@/assets/images/banner.jpg" alt="Banner" />

### 🔁 If you want to use it dynamically (for example in a loop):

<template>
  <img :src="getImageUrl('banner.jpg')" alt="Banner" />
</template>

<script setup>
function getImageUrl(filename) {
  return new URL(`@/assets/images/${filename}`, import.meta.url).href;
}
</script>

Let me know if you're referencing images dynamically (like from props or API), and I can help build that too.

User
Property 'URL' does not exist on type 'CreateComponentPublicInstanceWithMixins'.ts-plugin(2339)
Assistant
Ah — looks like you're trying to use `new URL(…)` **directly inside the template**, and TypeScript is complaining because it's not valid JavaScript inside a Vue template expression.

The key point is:

> **You can't use `new URL(…)` directly in the template markup.** You must declare it in “ or regular “ and then bind it in the template.

### ✅ Correct Way (Vite + Vue 3 + TypeScript)

Move the `new URL(…)` call into the “ section, like this:

<template>
  <img :src="bannerImage" alt="Banner" />
</template>

<script setup lang="ts">
const bannerImage = new URL('@/assets/images/banner.jpg', import.meta.url).href;
</script>

### ❌ Do NOT do this (causes TS error in template):

<!-- ❌ This is what causes the TS error -->
<template>
  <img :src="new URL('@/assets/images/banner.jpg', import.meta.url).href" alt="Banner" />
</template>

TypeScript can't interpret `new URL(…)` directly inside the template context — Vue's template compiler expects basic expressions, not constructors like `new`.

Let me know if you're passing image names as props or looping through images — we can make a reusable helper for that too.

Leave a Comment

Your email address will not be published. Required fields are marked *