Webpack & Typescript image import

后端 未结 3 644
醉话见心
醉话见心 2020-12-09 01:20

I\'m working on a React application and using Webpack & Typescript. I would like to use an image in one of the &

3条回答
  •  温柔的废话
    2020-12-09 02:08

    Alternatively, in your custom_typings folder (if you have that), you can add a new import-png.d.ts file:

    declare module "*.png" {
      const value: any;
      export default value;
    }
    

    So you can import an image using:

    import myImg from 'img/myImg.png';
    

    Alternatively, as reported by @mario-petrovic, you sometimes need to use a different export option as below (export = syntax). See here for the differences between the two approaches:

    declare module "*.png" {
      const value: any;
      export = value;
    }
    

    In which case you probably need to import the image as:

    import * as myImg from 'img/myImg.png';
    

提交回复
热议问题