Taro微信小程序报错:Failed to load local image resource的解决方案

场景

给taro地图组件添加标记的时候需要制定iconPath,然后报如下错误:

useEffect(() => {
    
    
    setCurrentMark([
      {
    
    
        ...
        iconPath: '../../../assets/bg-location.png',
        ...
      },
    ]);
  }, [currentLocation]);

[渲染层网络层错误] Failed to load local image resource
[渲染层错误] Uncaught Error: Uncaught TypeError: Cannot read property ‘x’ of undefined

找了很久,有说url格式错误的,有说基础库版本太高了的,基本上能找到的方法我都试过了,还是不行。于是只好自己试探着改一下,没想到成功了。

解决方案
import MarkerIcon from '../../../assets/bg-location.png'
...
iconPath: MarkerIcon ,
...

亲测能够解决。更奇葩地是,为了写这篇教程跑去复现error截图时竟然又不出来了,真是奇怪。

希望这个BUG能够在此贴终结吧!哈哈哈哈

猜你喜欢

转载自blog.csdn.net/weixin_43363720/article/details/129025649