Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue cli image wont load with webpack

Tags:

webpack

vue.js

What am I doing?

I am using the intersection observer API to make lazy loading.

What have I tried?

I tried the code in a simple HTML page and it works perfect, but when I use the code in vue, the images won't load (local images). If I put a htttp source images (online images) it works perfect, too. I think this is a webpack error config. Am I right? How can I fix it?.

Whats the error?

When i use a local image the code doesnt work, if only change that src with something else like this image https://images.pexels.com/photos/69817/france-confectionery-raspberry-cake-fruit-69817.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940 the code WORKS, why i cant make it work with local images?

HTML AND SCRIPT

<template>
    <div class="container" id="section3">
        <span class="containerTitle">Galeria</span>
        <div class="wrapper">
            <img v-lazyload data-src="@assets/images/001.jpg" class="card">
        </div>
    </div>
</template>
<script>
import lazyload from '../directives/lazyload'
export default {
    directives:{
      lazyload
    },   
}
</script>

DIRECTIVE

export default{
    inserted: el =>{
        const options = {
            // root:
            rootMargin: '0px 0px 0px 0px',
            threshold:1
        }
        var observer = new IntersectionObserver((entries,observer) =>{
            entries.forEach(entry => {
                if(entry.isIntersecting){
                    el.src = el.dataset.src
                    observer.unobserve(el)
                    console.log('intersecting');
                    
                }
            })
            },options) 
            observer.observe(el) 
    }
}

CODE IMAGE

enter image description here

FOLDER

enter image description here

like image 977
Alex Hunter Avatar asked Sep 10 '19 00:09

Alex Hunter


People also ask

Does vue CLI service use webpack?

It is the standard tooling baseline for using Vue JS which provides you with the Vue CLI service which is a runtime dependency, built on Webpack with 0 configs. It has a very extensive collection of plugins you can add to your project with a line of command.

How does webpack work with vue?

Webpack is a module bundler that takes your development assets (like Vue components and plain Javascript files) and combines them into so called bundles. This is different from simply concatenating and minifying JavaScript files. Simply put, with webpack bundles only the modules required on the current page are loaded.

How do I import an image into vue component?

To import and use image in a Vue. js single file component, we can set the src prop of the image to the image path returned by the require function. to set the src prop of the image to the path returned by require . We call require with the relative path of the image to return the resolved path of the image.


2 Answers

The issue is with your image path.

  1. You can fix it with either using public folder and give it in path.

  2. You can also check for auto suggestion which come up while typing, this may help you to check whether your path is correct or not.

Like this

like image 110
rushabh sojitra Avatar answered Sep 29 '22 12:09

rushabh sojitra


Your path is wrong. You gave ../assets/images/001.jpg as the path to the image (as stated in your question), but according to your directory tree it's ../assets/001.jpg (or write it like @/assets/001.jpg, @ points to root of project). That should fix it.

like image 22
Condor Avatar answered Sep 29 '22 10:09

Condor