在React JS中导入

时间:2016-05-25 13:26:23

标签: reactjs

之间有什么区别
import Something from 'react';

import {Something} from 'react';

这些花括号意味着什么?

1 个答案:

答案 0 :(得分:1)

import Something from 'react';

导入模块导出的default。 在这种情况下,导出应该像

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

import {Something} from 'react';导入命名导出,例如

export const Something = function(){}

如果您的模块同时具有default和命名导出,则可以将其导入为单个类似的导出。实施例

//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'