我正在尝试使用img标签中的src属性从本地文件夹加载图像,但我希望使用后端来加载它们。前端已经有了相对路径,即 "../assets/imgs/" ,而后端只有名称和扩展名,例如 "1.png"。虽然可以工作,但是我还是遇到了错误信息。
这正是困扰我的问题所在。
这是控制台显示的两个错误信息:
这正是困扰我的问题所在。
<img width=100px height=100px :src="getIconPath(`${user.icon}`)"/>
这是被调用的函数:
methods: {
getIconPath(iconName) {
return iconName ? require("../assets/imgs/" + iconName) : ''
}
这是控制台显示的两个错误信息:
[Vue warn]: Error in render: "Error: Cannot find module './undefined'"
found in
---> <Profile> at src/components/profile.vue
<Navbar> at src/components/navbar.vue
<Main> at src/main.vue
<Root> vue.runtime.esm.js:619
Error: "Cannot find module './undefined'"
webpackContextResolve .*$:13
webpackContext .*$:8
getIconPath profile.vue:74
render profile.vue:12
VueJS 43
<anonymous> main.js:31
js app.js:1415
__webpack_require__ app.js:785
fn app.js:151
1 app.js:1488
__webpack_require__ app.js:785
checkDeferredModules app.js:46
<anonymous> app.js:861
<anonymous>
我发现很少有资源可以帮助我,但其中许多人都说需要修复他们的问题。到目前为止,我尝试将它移动到另一个文件夹中,将require作为函数方法移动,使用绝对路径、使用v-attr以及不使用require进行绑定。但是我仍然没有摆脱错误消息的困扰。这里还有另一个链接,有人和我有相同的问题,但我仍然无法弄清楚他们是如何解决的。
非常感谢您的帮助!!我已经卡在这里很多个小时了,似乎找不到有用的解决方案。如果这不是从后端加载图像的好方法,请随时建议任何其他替代方法。谢谢!
user
变量从哪里来?它是异步加载的吗?如果是,那么它的初始数据是什么样子的(在被“加载”之前)? - PhilgetIconPath(user.icon)
,并且它正在加载而没有任何错误消息! - VictorgetIconPath(iconName) {
后面,你能否加上console.log(iconName)
? - foxiris