Vue中图片Src使用变量的方法
网络编程 2021-07-04 15:02www.168986.cn编程入门
这篇文章主要介绍了Vue中图片Src使用变量的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
相信不少同学在开发中都有遇到图片路径需要使用变量引入的情况,如定制化背景,动态展示头像等。可能也犯过如下错误
# 错误描述
页面直接调用图片资源的方案
<img src="../../static/images/web_bg.png" />
改写成变量形式,于是如下编写
<template>
<img :src="imgSrc" />
</template>
<script>
export default {
data() {
return {
imgSrc: '../../images/web_bg.png'
}
}
}
</script>
结果运行图片加载失败。什么原因?原来是因为在打包时会被自动加上hash值从而引用失败,产生差异
# 解决办法
1. 使用 网络上的图片资源
data() {
return {
imgSrc: 'http://easy-stage.longhu./files/images/7f458e55f6954078aa8e8efb2c4540.jpg'
}
}
2. 使用import导入本地资源
import imgSrc from '../../images/web_bg.png'
export default {
data() {
return {
imgSrc: imgSrc
}
}
}
3. 使用 require 导入
data() {
return {
imgSrc: require('../../images/web_bg.png')
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。