未知的命名模块

时间:2017-03-26 00:02:55

标签: react-native

我正在尝试创建一个动态图像按钮,所以我在“props”中传递图像的路径,但是我收到错误消息“Unknown named module”。 当我记录chrome控制台中的props.imageUrl时出现./test.png。如果我改为const icon = require('./test.png');效果很好!我做错了什么?

const ImageButton = (props) => {
  console.log(props.imageUrl);
  const icon = require(props.imageUrl);

  return (
    <TouchableOpacity onPress={props.onPress}>
      <Image
        source={icon}
      />
    </TouchableOpacity>
  );
};

1 个答案:

答案 0 :(得分:1)

由于打包程序的工作方式,这在React Native中是不可能的。您必须要求具有静态字符串路径的图像。如果可能,请将imageUrl媒体资源设为require('./test.png'),然后您可以<Image source={props.imageUrl} />