【问题标题】:How can I export images path with require() from one index.js file in react naitve?如何在 react naitve 中从一个 index.js 文件中使用 require() 导出图像路径?
【发布时间】:2022-01-06 11:22:17
【问题描述】:

我想从 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文件中导出和导入图片呢?

【问题讨论】:

    标签: javascript reactjs react-native image


    【解决方案1】:

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

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

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

    现在导入这个并使用 as-

    import imagePath from 'imagePath';
    
    return (
        <>
            <Image source={imagePath.logo} />
            <Image source={imagePath.newLogo} />
        </>
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-17
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2017-03-30
      • 1970-01-01
      相关资源
      最近更新 更多