'错误:未知命名模块',从动态路径加载react-native映像 [英] 'Error: Unknown named module', loading react-native image from a dynamic path
问题描述
当我在React-native中使用Image组件时,当我将图像的路径/源声明为内联字符串时,它可以正常工作:
When i use the Image component in React-native, it works fine when I declare my image's path/source as an inline string:
<Image
style={styles.img}
source={require('mypic.png')}
/>
但是当我将路径定义为这样的变量时:
But when I define the path as a variable like this:
var img = 'mypic.png';
<Image
style={styles.img}
source={require(img)}
/>
...它不起作用。错误消息是错误:未知的命名模块'mypic.png'
...it doesn't work. The error msg is "Error: unknown named module 'mypic.png'"
我有很多图像,我需要动态地要求它们。编写手动导入语句有太多要求逐个使用它们。
I have many images, and I need to require them dynamically. There are too many to write manual import statements to require them one-by-one.
即使使用像这样的简单切换,效率也会低得多:
Even with a simple toggle like this one, it's far less efficient:
var icon = this.props.active ? require('./my-icon-active.png') : require('./my-icon-inactive.png');
人们通常如何解决动态加载变量图像?
How do people usually solve for dynamic loading of variable images?
推荐答案
你不能做动态静态图像,所以你可以使用uri或做静态的东西,比如var测试=需要( '图像')。看看这个问题: https://github.com/facebook/react-native/ issue / 2481
You cannot do dynamic static images, so you can either use uri or do static things like var test=require('image'). Have a look at this issue: https://github.com/facebook/react-native/issues/2481
这篇关于'错误:未知命名模块',从动态路径加载react-native映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!