请大家一起来看看这些:什么时候应该写{}围绕导入的东西?
import React, { Component } from 'react';
import { AppRegistry, Text, View } from 'react-native';
正如你看到的,它进口的东西,但为什么React
超出{}
且其他所有成吗? {Component}
{ AppRegistry, Text, View }
无论如何,我应该什么时候把东西换成{}
?
请大家一起来看看这些:什么时候应该写{}围绕导入的东西?
import React, { Component } from 'react';
import { AppRegistry, Text, View } from 'react-native';
正如你看到的,它进口的东西,但为什么React
超出{}
且其他所有成吗? {Component}
{ AppRegistry, Text, View }
无论如何,我应该什么时候把东西换成{}
?
的差别,就在于文件的出口,没有{}
是默认的出口。只能有一个默认导出。
{}
中的任何内容都是导出的命名导出函数,类或变量的一部分。
如果你看看反应源代码,你会发现以下代码es5代码。
var ReactComponent = require('./ReactComponent');
...
var React = {
...
Component: ReactComponent,
...
}
module.exports = React;
当你import React, { Component }
您导入所有与React.Component一起阵营 作为组件
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
变为
class Welcome extends Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
这通常用来解构的对象,如以下。
const person = {
firstName: 'John',
lastName: 'Doe',
};
const { firstName } = person;
哪相同
person.firstName
谢谢.. upvote – stack
当你
export default MyComponent // import MyComponent
export MyComponent // import { MyComponent }
谢谢.. upvote。 – stack
也许理解[解构分配](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)可帮助 –
查看[本文]中的第3.3节(http://2ality.com/2014/09/es6-modules-final.html#having-both-named-exports-and-a-default-export-in-a -module) – sabithpocker