繁体   English   中英

如何在 react naitve 中从一个 index.js 文件中使用 require() 导出图像路径?

[英]How can I export images path with require() from one index.js file in react naitve?

我想从一个 index.js 文件中导出所有图像,这样如果将来我必须更改图像的路径,那么我只需要在一个文件中进行更改。

像这样:

export {default as avatar} from './avatar.png';
export {default as career} from './PsychoScreenImages/career.jpeg';
export {default as application} from './redeemIcons/application.jpeg';
export {default as featured} from './redeemIcons/featured.jpeg';

但我不能这样做require(avatar)因为它需要唯一的路径。 那么,如何从一个 js 文件中导出和导入图像呢?

您可以创建一个文件,该文件本身会导出其中所需的图像路径。 如下所示 -

你可以把这个文件命名为imagepaths.js

export default {
  logo: require('./images/logo.png'),
  newLogo: require('./images/logo-new.png')
}

现在导入它并用作-

import imagePath from 'imagePath';

return (
    <>
        <Image source={imagePath.logo} />
        <Image source={imagePath.newLogo} />
    </>
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM