我正在尝试通过执行以下操作将.png文件导入到我的react native组件中。
import background from '../../../assets/background';
import BannerLogo from '../../../assets/BannerLogo.png';
import SplashArt from '../../../assets/SplashArt.png';
我目前得到错误
assets/background.png:Cannot read property 'length' of undefined
终端中没有其他错误信息(或信息)。但是,这是我代码中唯一导入这些图像的地方。当我改变导入的顺序(例如BannerLogo首先),我得到相同的错误代码,但与
assets/BannerLogo.png
我使用世博SDK 40。这些图像的使用方式如下:
<ImageBackground
source={background}
style={{
width: '100%',
height: '100%',
flexGrow: 1,
alignItems: 'center',
justifyContent: 'center'
}}
imageStyle={{
resizeMode: 'repeat',
}}
/>
任何帮助都将不胜感激。
向源传递错误的值道具。
通过require('path')
或{uri: 'https://url'}
加载图像。
更多信息见官方文档
正确的代码应该是
<ImageBackground
source={require('../../../assets/background.png')}
style={{
width: '100%',
height: '100%',
flexGrow: 1,
alignItems: 'center',
justifyContent: 'center'
}}
imageStyle={{
resizeMode: 'repeat',
}}
/>