【问题标题】:Importing in React JS [duplicate]在 React JS 中导入 [重复]
【发布时间】:2016-09-23 03:31:22
【问题描述】:

有什么区别

import Something from 'react';

import {Something} from 'react';

这些花括号是什么意思?

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    import Something from 'react';

    导入模块导出的default 是什么。 在这种情况下,导出应该是这样的

    export default const Something = function(){...}

    import {Something} from 'react'; 导入一个命名的导出,比如

    export const Something = function(){}

    如果您的模块同时具有default 和命名导出,您可以将它们导入一个like。示例

    //module A
    export default const Something = function(){}
    export const SomethingElse = function(){}
    

    然后像这样导入它们

    //module B
    
    import Something, { SomethingElse } from 'moduleA';
    

    之前的default不必导入为Something,你可以用任何你想要的名字导入。

    import A from 'moduleA'

    等于

    import Something from 'moduleA'

    【讨论】:

    • 您还可以在同一行中提取命名导出。例如:import React, { Component } from 'react';
    • @JoshuaSlate 我正在更新答案
    猜你喜欢
    • 2018-04-04
    • 2017-10-10
    • 2018-04-27
    • 1970-01-01
    • 2020-12-29
    • 2022-07-30
    • 2020-10-16
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多