Vue Image Loader Component
The `vue-load-image` package provides a minimalist Vue.js component designed to manage the loading state of images, enhancing user experience by displaying a preloader while an image fetches. This `0.2.0` version is specifically built for Vue 2.x applications. It allows developers to define custom content for the loading state (preloader) and an error state (alternate content) if the image fails to load. Weighing in at just 1KB gzipped, it's a very lightweight solution focused solely on image loading feedback. However, with its last substantial update being over five years ago, coinciding with the shift to Vue CLI, and its peer dependency on Vue 2.x (which reached End-of-Life in December 2023), this particular version is considered abandoned. While a `@next` tag exists for Vue 3, this entry pertains to the Vue 2 compatible `0.2.0` release. Key differentiators include its simplicity and direct slot-based customization for loading and error states, rather than complex lazy-loading or advanced progressive image techniques.
Common errors
-
[Vue warn]: Unknown custom element: <vue-load-image>
cause The `vue-load-image` component was not properly imported or registered in the Vue component where it's being used.fixEnsure you have `import VueLoadImage from 'vue-load-image';` in your script and `components: { 'vue-load-image': VueLoadImage }` in your component's options or registered globally using `Vue.component('vue-load-image', VueLoadImage);` in your main.js file. -
GET http://localhost:8080/image.png 404 (Not Found)
cause The image `src` provided to the `img` tag within the `image` slot is incorrect or the image file does not exist at the specified path, especially when dealing with local assets in a Vue CLI project.fixDouble-check the image path. For images in `src/assets`, use `require('./assets/image.png')` or import them directly. For images in the `public` directory, use an absolute path like `/image.png`. Verify the image file exists and its filename matches exactly. -
The preloader doesn't appear, or the image just flashes briefly before loading.
cause The preloader image/content is either not styled correctly to be visible, or the main image loads too quickly for the preloader to be meaningfully displayed, often due to local caching or very fast network conditions.fixEnsure your preloader content has distinct styling (e.g., background color, `width`, `height`, `text-align: center`). You can temporarily add a `setTimeout` around the image `src` assignment in the component's internal logic (if modifying source) or simulate network delay in browser developer tools to observe the preloader behavior. For very fast loads, the preloader might intentionally be skipped or barely visible as part of a good UX.
Warnings
- breaking This version (0.2.0) is built for Vue 2.x, which reached End-of-Life (EOL) in December 2023. Using this package in a new Vue 3 project will lead to incompatibility and runtime errors due to breaking changes in Vue's API between major versions.
- gotcha Dynamic image paths (e.g., from data or props) in Vue 2 often require specific handling with `require()` or `new URL()` within templates or script blocks, especially when images are located in the `src/assets` directory and processed by webpack/Vue CLI. Directly using a string path like `src="./assets/my-image.png"` might not work in production builds.
- deprecated The component itself shows no active maintenance for the Vue 2.x branch since approximately five years ago, aligning with the end of Vue 2 support. This means no new features, bug fixes, or security patches will be released for this version.
- gotcha Styling the preloader and error slots requires careful consideration. The default component provides structural slots, but no inherent styling. Developers must apply their own CSS to ensure the loader is visible, centered, and visually appealing, and that the error message is clearly formatted.
Install
-
npm install vue-load-image -
yarn add vue-load-image -
pnpm add vue-load-image
Imports
- VueLoadImage
import { VueLoadImage } from 'vue-load-image';import VueLoadImage from 'vue-load-image';
- Component Registration (Local)
export default { components: { VueLoadImage } }export default { components: { 'vue-load-image': VueLoadImage } } - Component Registration (Global)
Vue.component(VueLoadImage.name, VueLoadImage);
import VueLoadImage from 'vue-load-image'; Vue.component('vue-load-image', VueLoadImage);
Quickstart
<!-- App.vue (or any Vue 2 component) -->
<template>
<div id="app">
<h1>Image Loading Demo</h1>
<p>Using vue-load-image to display a loader while images load.</p>
<h2>Example 1: Basic Image with Preloader and Error Slot</h2>
<vue-load-image>
<template v-slot:image>
<img src="https://via.placeholder.com/400x200?text=Loaded+Image" alt="Loaded Image" />
</template>
<template v-slot:preloader>
<img src="https://i.imgur.com/gKj31sO.gif" alt="Loading..." style="width: 100px; height: 100px;" />
</template>
<template v-slot:error>
<div style="color: red; border: 1px solid red; padding: 10px;">Failed to load image!</div>
</template>
</vue-load-image>
<h2>Example 2: Image with an intentionally broken URL</h2>
<vue-load-image>
<template v-slot:image>
<img src="/non-existent-image.jpg" alt="Broken Image" />
</template>
<template v-slot:preloader>
<p>Loading broken image...</p>
</template>
<template v-slot:error>
<div style="background-color: #ffe0e0; padding: 10px;">Oops! This image couldn't be found.</div>
</template>
</vue-load-image>
</div>
</template>
<script>
import VueLoadImage from 'vue-load-image';
export default {
name: 'App',
components: {
'vue-load-image': VueLoadImage // Registering the component locally
}
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
.vue-load-image {
display: inline-block;
margin: 20px;
border: 1px solid #ccc;
padding: 10px;
}
</style>