Several ways to use require/import keywords to import local images in v-for loop

Several ways to use require/import keywords to import local images in v-for loop

Problem Description

When we do projects, we often need to present pictures on the page. Generally speaking, there are the following ways

Method 1 (backend returns image URL)

This method is that the backend returns the URL address of the image, and we directly bind the src attribute of the img tag to imgUrl. The following code:

<div class="item" v-for="(item, index) in apiArr" :key="index">
    <!-- apiArr is the data returned by the backend, each of which has an imgUrl attribute that stores the URL address of the image -->
    <img :src="item.imgUrl" alt="">
</div>

Method 2 (using require on the front end)

The second method is to store the image file in the front end, and the back end only returns the name of the image (or does not return the image data). The code example is as follows:

Code attached

<template>
  <div class="wrap">
    <div class="item" v-for="(item, index) in apiArr" :key="index">
      <div class="imgWrap">
        <!-- require introduces the image file module -->
        <img :src="require(`@/assets/img/${item.imgTitle}.png`)" alt="" />
        <!-- Finally it will become like this and can be displayed normally <img src="@/assets/img/first.png" alt=""> -->
      </div>
      <div class="infoWrap">
        <div><span class="bloder">Rank:</span> {{ item.title }}</div>
        <div><span class="bloder">Score:</span> {{ item.score }}</div>
      </div>
    </div>
    
  </div>
</template>

<script>
export default {
  data() {
    return {
      apiArr: [],
    };
  },
  mounted() {
    // Assume that apiArr is the data returned by the backend when we send the request, and the imgTitle property in it stores the name of the image // By introducing it through the require keyword, it will automatically search for the corresponding image file in the file under the specified path and load it out this.apiArr = [
      {
        title: "Champion",
        score: "98.8",
        imgTitle: "first",
      },
      {
        title: "Runner-up",
        score: "97.9",
        imgTitle: "second",
      },
      {
        title: "Third runner-up",
        score: "96.2",
        imgTitle: "third",
      },
    ];
  },
};
</script>

The effect diagram is as follows

Project file structure diagram

Method 3 (using import on the front end)

Code attached

<template>
  <div class="wrap">
    <div class="item" v-for="(item, index) in apiArr" :key="index">
      <div class="imgWrap">
        <img :src="item.imgTitle" alt="" />
      </div>
      <div class="infoWrap">
        <div><span class="bloder">Rank:</span> {{ item.title }}</div>
        <div><span class="bloder">Score:</span> {{ item.score }}</div>
      </div>
    </div>
  </div>
</template>

<script>
// import import first from "@/assets/img/first.png";
import second from "@/assets/img/second.png";
import third from '@/assets/img/third.png'
export default {
  data() {
    return {
      apiArr: [
        {
          title: "Champion",
          score: "98.8",
          imgTitle: first, // Use the imported image},
        {
          title: "Runner-up",
          score: "97.9",
          imgTitle: second, // Use the imported image},
        {
          title: "Third runner-up",
          score: "96.2",
          imgTitle: third, // Use the imported image},
      ],
    };
  },
};
</script>

The renderings and project file structure diagram are the same as above, so I won’t go into details here.

Summarize

You can use the import method in ES6 or the require method in commonjs to import images, but I personally recommend using the require method because it is slightly more flexible.

This is the end of this article about using require/import keywords in v-for loop to introduce local images. For more relevant v-for content about introducing local images, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of several ways to introduce image paths in Vue.js
  • How to introduce vue-router in requireJs of vue series
  • Vue's images need to be introduced using require

<<:  MySQL 8.0.20 Installation Tutorial with Pictures and Text (Windows 64-bit)

>>:  How to start the spring-boot project using the built-in linux system in win10

Recommend

Creating Responsive Emails with Vue.js and MJML

MJML is a modern email tool that enables develope...

JavaScript object built-in objects, value types and reference types explained

Table of contents Object Object Definition Iterat...

Practical method of deleting associated tables in MySQL

In the MySQL database, after tables are associate...

MySQL index usage monitoring skills (worth collecting!)

Overview In a relational database, an index is a ...

How to use vs2019 for Linux remote development

Usually, there are two options when we develop Li...

Vue Element front-end application development table list display

1. List query interface effect Before introducing...

Linux Operation and Maintenance Basic System Disk Management Tutorial

1. Disk partition: 2. fdisk partition If the disk...

In-depth understanding of the seven communication methods of Vue components

Table of contents 1. props/$emit Introduction Cod...

Analysis of the principle of Rabbitmq heartbea heartbeat detection mechanism

Preface When using RabbitMQ, if there is no traff...

Summary of JavaScript's setTimeout() usage

Table of contents 1. Introduction 2. The differen...

Navigation Design and Information Architecture

<br />Most of the time when we talk about na...

JS deep and shallow copy details

Table of contents 1. What does shallow copy mean?...

Summary of changes in the use of axios in vue3 study notes

Table of contents 1. Basic use of axio 2. How to ...