简体   繁体   English

无法在本机反应中加载静态图像

[英]Unable to load static images in react native

My folder structure :我的文件夹结构:

-assets -资产

--images - 图片

---potrait.png ---肖像.png

-src -src

--file.js --file.js

I want to load my image in file.js but it throws error我想在 file.js 中加载我的图像,但它引发错误

<Image source={require('../assets/images/potrait.png')} />

Error:错误:

error: Error: Unable to resolve module ../assets/images/potrait.png from D:\\munish\\Desktop\\Invictus\\reach-app\\src\\reactNativeCamera.js:错误:错误:无法从 D:\\munish\\Desktop\\Invictus\\reach-app\\src\\reactNativeCamera.js 解析模块 ../assets/images/potrait.png:

None of these files exist:这些文件都不存在:

  • potrait.png肖像.png
  • assets\\images\\potrait.png\\index(.native|.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx) assets\\images\\potrait.png\\index(.native|.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|. ts|.android.tsx|.native.tsx|.tsx)

Sadly, react disables imports outside of src.可悲的是,react 禁用了 src 之外的导入。 You should move them to inside the src or to the public folder where it can be accessed through / ( not recommended because it will replicate the image, should be avoided for the best optimization ).您应该将它们移动到 src 内部或可以通过/访问它的public文件夹(不推荐,因为它会复制图像,应避免进行最佳优化)。

你试过这个语法吗?

<Image source={{uri: localPath}}/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM