Home > Web Front-end > Vue.js > What to do if the vue.js image resource path is incorrect

What to do if the vue.js image resource path is incorrect

藏色散人
Release: 2023-01-13 00:45:05
Original
2395 people have browsed it

The solution to the incorrect path of vue.js image resource: 1. Use v-bind to bind the src attribute of the image; 2. Use import to introduce the image path; 3. Place the image in the outer static folder inside, and then define imgUrl in data.

What to do if the vue.js image resource path is incorrect

The operating environment of this tutorial: windows7 system, vue2.0 version, DELL G3 computer. This method is suitable for all brands of computers.

[Related article recommendations: vue.js]

When we reference images in the Vue.js project, there are the following situations regarding the image path:

Use one:

Define the image path in the data:

 /*错误写法*/    imgUrl:'../assets/logo.png'
Copy after login

In the template:

/*错误写法*/   <img src="{{imgUrl}}">
Copy after login

The above is an incorrect way of writing, we should use v- bind binds the src attribute of the image:

<img :src="imgUrl">
//或者 
<img src="../assets/logo.png">
Copy after login

This method refers to the path according to normal HTML syntax, and can be packaged by webpack when placed in the template.

Use 2:

When we need to write the image path in the js code, if we write it in the data, webpack will only treat it as a string and cannot find the image address. ,

Wrong writing method

imgUrl:&#39;../assets/logo.png&#39;
Copy after login

At this time we can use import to introduce the image path:

<img :src="imgUrl" />
import avatar from &#39;@/assets/logo.png&#39;
Copy after login

Definition in data: avatar: avatar

Use three:

We can also put the image in the outer static folder, and then define it in data:

imgUrl : &#39;../../static/logo.png&#39;
<img :src="imgUrl" />
Copy after login

The above is the detailed content of What to do if the vue.js image resource path is incorrect. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template