【问题标题】:Javascript/Typescript - Different syntax to import classJavascript/Typescript - 导入类的不同语法
【发布时间】:2019-10-09 13:14:34
【问题描述】:

在 Angular 中导入的两种语法有什么不同:

import partition from 'lodash/fp/partition';
import { partition } from 'lodash/fp/partition';

第一个正在工作,但另一个不适合我。

谢谢

【问题讨论】:

  • 为了您的澄清,这不是Angular 功能。这是javascript ES6 功能。

标签: javascript typescript lodash


【解决方案1】:

第一个用于导入标记为default的东西:

// lodash/fp/partiotion

export const a = 5;
export const b = 'b';
export default {key: 'value'}; // only this line will be imported using the first syntax

第二个允许您指定要导入的内容:

// lodash/fp/partiotion

export const a = 5;
export const b = 7;
export const partition = () => {}; // you are directly importing only this line

【讨论】:

  • 有关如何使用导入检查 MDN 文档here的更多详细信息。
【解决方案2】:

第一个从模块 'lodash/fp/partition' 导入默认导出,而第二个尝试解构模块的导出并仅获取 partition 导出。

对于这个模块:

module.exports = {
  default: 1,
  partition: 2,
}

从'lodash/fp/partition'导入分区;应该是:

partion === 1

从'lodash/fp/partition'导入{分区};应该是:

partion === 2

对于这个模块:

module.exports = 1

从'lodash/fp/partition'导入分区;应该是:

partion === 1

从'lodash/fp/partition'导入{分区};应该是:

partion === undefined

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 2016-10-29
    • 2017-12-03
    • 2016-07-17
    • 2019-08-22
    • 2018-10-30
    • 2023-02-22
    • 2016-04-04
    • 1970-01-01
    相关资源
    最近更新 更多