这是因为create-react-app默认使用了Babel 7,它使用了不同的配置文件。在这种情况下,只需将组件导出并在应用程序中导入。例如,考虑以下情况:
src/components/MyComponent.js
import React, { Component } from 'react';
class MyComponent extends Component {
render() {
return (
Hello World!
);
}
}
export default MyComponent;
src/App.js
import React, { Component } from 'react';
import MyComponent from './components/MyComponent';
class App extends Component {
render() {
return (
);
}
}
export default App;
在这里,我们只需导出MyComponent并在App.js中导入它。在运行npm start命令后,create-react-app会将MyComponent编译为可在浏览器中运行的代码。