vue中img的src动态渲染不显示问题
Posted zixuan00
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue中img的src动态渲染不显示问题相关的知识,希望对你有一定的参考价值。
在vue中写动态展示图标的需求时,遇到个小坑:
需求:(根据权限动态展示图标,如果没有显式默认图标)
![技术图片](https://image.cha138.com/20210625/ad3814fa0f2b40528f8c72762460660b.jpg)
错误写法1:
![技术图片](https://upload-images.jianshu.io/upload_images/20583305-a470b33a841d7505.png?imageMogr2/auto-orient/strip|imageView2/2/w/480/format/webp)
错误写法2:
![技术图片](https://upload-images.jianshu.io/upload_images/20583305-3b9c7fcfee604859.png?imageMogr2/auto-orient/strip|imageView2/2/w/521/format/webp)
报错:(实际img文件夹是有这些图片的,没找到)
![技术图片](https://upload-images.jianshu.io/upload_images/20583305-7cb6a1b7ce8de116.png?imageMogr2/auto-orient/strip|imageView2/2/w/509/format/webp)
审查元素发现:(图片并没有被打包,显示引入路径)
![技术图片](https://upload-images.jianshu.io/upload_images/20583305-5f13111431c63ea6.png?imageMogr2/auto-orient/strip|imageView2/2/w/532/format/webp)
解决办法:
方法一: 使用require引入图片:
![技术图片](https://upload-images.jianshu.io/upload_images/20583305-75d888854489df73.png?imageMogr2/auto-orient/strip|imageView2/2/w/545/format/webp)
方法二: 使用img的onerror属性:
![技术图片](https://upload-images.jianshu.io/upload_images/20583305-80629c965cfdc9f6.png?imageMogr2/auto-orient/strip|imageView2/2/w/566/format/webp)
审查元素已经被成功打包转为base64:
以上是关于vue中img的src动态渲染不显示问题的主要内容,如果未能解决你的问题,请参考以下文章