Home  >  Article  >  Web Front-end  >  How to insert pictures in vue.js

How to insert pictures in vue.js

藏色散人
藏色散人Original
2020-12-14 10:41:292968browse

The method of inserting images in vue.js: first bind variables to the image address; then set the variables in the script; finally introduce the image as a module through the require method or put imgUrl in the data.

How to insert pictures in vue.js

The operating environment of this tutorial: windows7 system, vue2.0 version, thinkpad t480 computer.

Recommended: "vue tutorial"

vue insert pictures

How to insert pictures in vue.js

First give Image address binding variable

<template>
    <img  :src="imgUrl" alt="How to insert pictures in vue.js" >
</template>

Set variables in script

<script>
    //方法1.直接将图片引入为模块
    require imgUrl from "../assets/test.png"
 
    //方法2.将imgUrl放在数据里
    data(){
        return {
            imgUrl:require("../assets/test.png")
        }
    }
 
    //方法3.在生命周期函数中设置
 
    data(){
        return {
            imgUrl:""
        }
    }
 
    created(){
        let urlTemp = "assets/test.png";
        this.imgUrl = require("@/"+urlTemp)
    }
</script>

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to insert pictures in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:What is vue.js pluginNext article:What is vue.js plugin