React Native 图像不存在错误

agyaoht7  于 2023-03-24  发布在  React
关注(0)|答案(2)|浏览(167)

我从React Native官方教程中引用了ScrollView
图像代码就像<Image source={require('./img/favicon.png')} />
当我试着建造它时,它显示出
开发服务器返回响应错误代码:500 URL:http://10.0.2.2:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false正文:

{"from":"F:\\ReactNativeProject\\FirstProject\\index.android.js","to":"./img/favicon.png","message":"Unable to resolve module `./img/favicon.png` from `F:\\ReactNativeProject\\FirstProject\\index.android.js`: Directory F:\\ReactNativeProject\\FirstProject\\img\\favicon.png doesn't exist","name":"UnableToResolveError","type":"UnableToResolveError","errors":[{}]}

我将图像设置为我的文件夹F:\ReactNativeProject\FirstProject\img
图像名称为favicon
为什么显示错误?有人能告诉我为什么吗?谢谢。
新错误为F:/ReactNativeProject/FirstProject/index.android.js: Unexpected token (9:25) at F:\ReactNativeProject\FirstProject\index.android.js:9:25
我的密码是

import React, { Component } from 'react';
import{ AppRegistry, ScrollView, Image, Text, View } from 'react-native'

class IScrolledDownAndWhatHappenedNextShockedMe extends Component {
  render() {
      return(
        <ScrollView>
<Image source={../../img/favicon.png} />
</ScrollView>
    );
  }
}

AppRegistry.registerComponent('FirstProject', () => IScrolledDownAndWhatHappenedNextShockedMe);
prdp8dxp

prdp8dxp1#

require是用来引用一个模块/文件。因此你得到的错误。

<Image source={require('./img/favicon.png')} />

应为:

<Image source={path/favicon.png} />//path=path of image
ycl3bljg

ycl3bljg2#

您可以使用此解决方案解决此问题

{data?.image ?
                                        <View>
                                            <Image
                                                source={{ uri: data?.image ? data?.image : "" }}
                                            />
                                        </View> : null}

相关问题