代码之家  ›  专栏  ›  技术社区  ›  pashute

es6如何导入已在另一个js包中定义的

  •  0
  • pashute  · 技术社区  · 7 年前

    我有一个这样的包:

    import { something } from 'somewhere';
    

    但是我有另一个导入的包,我需要定义相同的包 something 其中定义的名称。

    import myConsts from 'SomewhereElse';
    const { something, another } = myConsts;
    

    我得到一个eslint错误(没错) something already defined .

    下面是一个真实的例子:

    import { connect } from 'react-redux';
    // following lines from react-native-kontaktio sample code...
    import Kontakt from 'react-native-kontaktio';
    const { connect, configure, startScanning } = Kontakt;
    

    我试过了

    import { connect as kontaktConnect, configure, startScanning } from 'react-native-kontaktio'  
    

    但是得到 Possible promise rejection ... (reactNativeKontaktio.connect) is not a function .

    如果我试图改变

    import { connect as reduxConnect } from 'react-redux';
    

    我得把出口改成如下。这不会破坏我在别处的代码吗?

    // export default connect(mapStateToProps, mapDispatchToProps)(AppMain);
    export default reduxConnect(mapStateToProps, mapDispatchToProps)(AppMain);
    

    我怎样才能克服这一点?在某些情况下我可以忽略警告吗?Ecma6没有多态性对吧?


    这不是 a question about two classes with the same name ,但大约有两个具有相同名称的方法或常量的类。 这里的答案似乎适用于:

    // instead of: import myConsts from 'SomewhereElse';
    import { something as somethingElse, another } from 'SomewhereElse';
    

    但是当我用。。。 somethingElse().then(()=> ... 我有个错误 Possible promise rejection ... (SomewhereElse.something) is not a function

    这也不是关于修复将军的问题。 is already defined eslint错误]( Javascript standardjs - how to fix 'is already defined'? ),因为我不是在谈论编写代码,而是如何 进口和使用 其他人的两个包裹有冲突问题。

    1 回复  |  直到 7 年前
        1
  •  4
  •   Filipe Doutel Silva    7 年前

    不,我认为您不能忽略这个警告,因为在同一范围中存在两个同名的变量。

    您可能需要以这种方式导入:

    import { connect as somethingElse} from 'react-redux';
    

    避免使用相同名称的变量。

    希望对你有帮助

    推荐文章