How to import two classes by the same name in javascript/es6?

后端 未结 2 468
再見小時候
再見小時候 2020-12-03 04:37

I have these two import statements in file:

import Data from \'component/Data.js\';
import Data from \'actions/Data.js\';

Both files contai

相关标签:
2条回答
  • 2020-12-03 04:51

    Presumably component/Data and actions/Data both have default exports rather than named exports? Like this:

    export default class Data {}
    

    If that's the case, then the importer can call the variables whatever they like:

    import Data1 from 'component/Data.js';
    import Data2 from 'actions/Data.js';
    

    If they are named exports:

    export class Data {}
    

    Then you need to use braces along with as to specify the source and target names:

    import { Data as Data1 } from 'component/Data.js';
    import { Data as Data2 } from 'actions/Data.js';
    
    0 讨论(0)
  • 2020-12-03 04:51

    EDITED: As per RGraham answer, updating my answer:

    Can't you import it like this:

    import {Data as D1} from 'component/Data.js';
    import {Data as D2} from 'actions/Data.js';
    

    Then use it as you require:

    D1.{}
    D2.{}
    

    referenced from: https://github.com/lukehoban/es6features/blob/master/README.md/#user-content-modules

    0 讨论(0)
提交回复
热议问题