一、问题描述
在vue项目中,想要动态设置img的src时,此时发现图片会加载失败。在Vue代码中是这样写的:

在Vue的data中是这样写的:

我的图片在根目录下的static里面:

但是在页面上这个图片却无法加载出来。

二、解决方案
原因:src被当做静态资源处理了,并没有进行编译。

解决方式:使用require引入图片,因为require是在运行时加载。如下:

界面效果:

在vue项目中,想要动态设置img的src时,此时发现图片会加载失败。在Vue代码中是这样写的:
在Vue的data中是这样写的:
我的图片在根目录下的static里面:
但是在页面上这个图片却无法加载出来。
原因:src被当做静态资源处理了,并没有进行编译。
解决方式:使用require引入图片,因为require是在运行时加载。如下:
界面效果: