简体   繁体   English

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

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

I want to export all images from one file which index.js so that If in future I have to change the path of image then I have to only make changes in one file.我想从一个 index.js 文件中导出所有图像,这样如果将来我必须更改图像的路径,那么我只需要在一个文件中进行更改。

like this:像这样:

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';

But I can't do like this require(avatar) as it requires the only a path.但我不能这样做require(avatar)因为它需要唯一的路径。 So, How can I export and import images from just one js file?那么,如何从一个 js 文件中导出和导入图像呢?

You can create a file that itself exports the image paths require within it.您可以创建一个文件,该文件本身会导出其中所需的图像路径。 As shown below -如下所示 -

Just you can name this file imagepaths.js你可以把这个文件命名为imagepaths.js

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

Now import this and use as-现在导入它并用作-

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