导入React和导入{Component}语法之间的区别 [英] Difference between import React and import { Component } syntax

查看:521
本文介绍了导入React和导入{Component}语法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我们正在将React与ES6一起使用.我们将React和Component导入为

Say, we are using React with ES6. We import React and Component as

import React from 'react'
import { Component } from 'react'

为什么语法不同?我们不能按以下规定使用吗?

Why the syntax difference? Can't we use as specified below?

import Component from 'react'

推荐答案

以下是文档,用于import.

import React from 'react'

以上是默认导入.默认导入使用export default ...导出.默认只能导出一个.

The above is a default import. Default imports are exported with export default .... There can be only a single default export.

import { Component } from 'react'

但这是成员导入(命名为import).成员导入使用export ...导出.可以有很多会员出口.

But this is a member import (named import). Member imports are exported with export .... There can be many member exports.

您可以使用以下语法导入这两者:

You can import both by using this syntax:

import React, { Component } from 'react';

在JavaScript中,默认导入和命名导入是分开的,因此您不能像默认导入那样导入命名导入.下面,将名称Component设置为'react'程序包的默认导出(它将与React.Component相同:

In JavaScript the default and named imports are split, so you can't import a named import like it was the default. The following, sets the name Component to the default export of the 'react' package (which is not going to be the same as React.Component:

import Component from 'react';

这篇关于导入React和导入{Component}语法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆