React Native - 使用动态名称的图像需求模块

2023-11-29

我目前正在使用 React Native 构建一个测试应用程序。到目前为止,图像模块运行良好。

例如,如果我有一个名为avatar,下面的代码片段工作正常。

<Image source={require('image!avatar')} />

但是如果我将其更改为动态字符串,我会得到

<Image source={require('image!' + 'avatar')} />

我收到错误:



Requiring unknown module "image!avatar". If you are sure the module is there, try restarting the packager.
  

显然,这是一个人为的示例,但动态图像名称很重要。 React Native 不支持动态图片名称吗?

React native error with dynamic image name


文档中的“静态资源":

引用捆绑包中的图像的唯一允许的方法是在源中逐字编写 require('image!name-of-the-asset') 。

// GOOD
<Image source={require('image!my-icon')} />

// BAD
var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive';
<Image source={require('image!' + icon)} />

// GOOD
var icon = this.props.active ? require('image!my-icon-active') : require('image!my-icon-inactive');
<Image source={icon} />

但是,您还需要记住将图像添加到 Xcode 中应用程序的 xcassets 包中,尽管从您的评论看来您已经这样做了。

http://facebook.github.io/react-native/docs/image.html#adding-static-resources-to-your-app-using-images-xcassets

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React Native - 使用动态名称的图像需求模块 的相关文章

随机推荐