Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript
  • Vue.js
  • D3.js
  • Collect.js
  • Underscore.js
  • Moment.js
  • Ember.js
  • Tensorflow.js
  • Fabric.js
  • JS Formatter
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to loop through a list of elements and display it in VueJS ?
Next article icon

How to check an image is loaded or not in VueJS ?

Last Updated : 25 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

While inserting an Image in a VueJS project, it may fail to load due to the following reasons:

  • Writing wrong image URL.
  • Due to poor connection

Approach: We are going to use an event in <img> to check whether an image is loaded or not in VueJS, the event we are going to use is:

  • @load: The @load event gets triggered when the image is loaded and it gets executed.

Project Setup:

Step 1: Create a Vue Project using the following command in the command line:

vue create image-load

Note: We have taken 'image-load' as the name of the Project, you can choose any name according to your choice.

  • The 'image-load' Folder will be created.
  • Open the folder in your Code Editor.

The project structure will look like this:

Step 2: After creating the project, add an image in the 'assets folder'. We have added an image with the name - 'gfg.png'

Example: In this example, we are going to follow the following steps:

  1. In this example, we're going to Insert an Image on the index page of our application.
  2. In the project "image-load", we create a data variable 'isLoaded' which has the default value "False".
  3. A 'description' data variable is also created which holds the Heading of the Page i.e "How to check if an image is loaded in VueJs?".
  4. Assign a '@load' event to the Image.
  5. The name of the event will be 'loadImage', and its function will be to change the value of 'isLoaded' to "True" if the image gets loaded.
  6. And at last, we will print the value of 'isLoaded' on our homePage, with the Image.

Now let's see each step one by one with the implementation.

App.vue
<template>     <div>         <h1>{{description}}</h1>         <br>          <img src="./assets/gfg.png" alt="" @load="loadImage">         <h2>Image Loaded : {{isLoaded}} </h2>     </div> </template>  <script>     export default {         name: 'App',         data() {             return {                 description: "How to check if Image "                     + "is Loaded in Vue.js or not?",                 isLoaded: false,             };         },          methods: {             loadImage() {                 this.isLoaded = true;             }         }     } </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;     } </style> 

Run the application: In the command line, enter the following command:

npm run serve

Output: Open the browser and go to http://localhost:8080/  and you will see the following output:

Explanation: As we can see, we had Initialised "isLoaded" to False initially. The image gets loaded and 'isLoaded' is assigned a True value and 'isImageLoaded' value is then fetched and displayed with the Image during Output.


Next Article
How to loop through a list of elements and display it in VueJS ?
author
vaibhavpatel1904
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Vue.JS
  • JavaScript-Questions

Similar Reads

  • How to replace all occurrences of a string using Vue.js filters ?
    In this article, we are going to learn how to replace all occurrences of a particular word with some other word using filters in VueJS. Vue is a progressive framework for building user interfaces. Filters are a functionality provided by Vue components that let you apply formatting and transformation
    2 min read
  • How to set a click event once a page or view is loaded in vue.js?
    In this article, we will discuss how to set a click event once a page or view is loaded in vue.js. Just like every other JavaScript framework Vue also supports the Mounting hooks. Mounting hooks are often the most used hooks. They allow us to access or modify the DOM of your component just before or
    2 min read
  • How to check an image is loaded or not in VueJS ?
    While inserting an Image in a VueJS project, it may fail to load due to the following reasons: Writing wrong image URL.Due to poor connection Approach: We are going to use an event in <img> to check whether an image is loaded or not in VueJS, the event we are going to use is: @load: The @load
    3 min read
  • How to loop through a list of elements and display it in VueJS ?
    Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supportin
    2 min read
  • How to Make localStorage Reactive in Vue.js ?
    In Vue, localStorage is used to store data locally in the browser. However, by default, changes made to localStorage are not reactive in Vue. This means that if the localStorage is updated outside of the Vue component, the component will not be aware of the changes and will not update accordingly. T
    3 min read
  • How to implement DateTime localization in vue.js ?
    In this article, we will see the implementation of Date Time localization in Vue.js, along with knowing its basic implementation through the illustration. Date() Object: In Javascript, the Date object works with dates and times. Date objects are created by the new Date() constructor. Syntax: const d
    8 min read
  • Select different values on two select tags in Vue.js
    In this article, we will see the selection of different values on a single select and two <select> tags in vue.js, along with understanding their basic implementation through the examples. HTML <select> tag is used to create a drop-down list. Adding a list of options inside the drop-down
    6 min read
  • How to convert a normal string to a uppercase string using filter in VueJS ?
    Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned value is t
    2 min read
  • How to dynamically add or remove items from a list in Vue.js ?
    Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supportin
    1 min read
  • How to binding inline styles in Vue.js ?
    Vue.js is an open-source Model–View–ViewModel front-end JavaScript framework for building user interfaces and single-page applications. Every Web application needs HTML CSS styles for presenting a better User Interface. Vue.js allows you to dynamically render styling with data model structure. Usual
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences