导入已安装的节点模块与导入 css 或 React 中的其他文件
Importing installed node modules vs importing css or other files in React
有人可以解释为什么会这样吗:
import React from '../node_modules/react';
等同于:
import React from 'react';
还有这个:
import './App.css';
不同于:
import 'App.css'; or import 'App.css';
在后面的示例中,我收到一条消息,指出在 src 目录中找不到文件 App.css,但它在那里。
它通过名称识别模块
但是它只能通过路径检测文件
正如您将在 the documentation
中看到的
module-name
The module to import from. This is often a relative or absolute path name to the .js file containing the module. Certain bundlers may permit or require the use of the extension; check your environment. Only single quoted and double quoted Strings are allowed.
name
Name of the module object that will be used as a kind of namespace when referring to the imports.
无需指定路径即可导入已安装的节点模块。
App.css不是节点模块,因此需要指定路径
自 create react app V3 发布以来,您还可以输入绝对路径,而不是有时令人困惑的方法 './../file.js'
有人可以解释为什么会这样吗:
import React from '../node_modules/react';
等同于:
import React from 'react';
还有这个:
import './App.css';
不同于:
import 'App.css'; or import 'App.css';
在后面的示例中,我收到一条消息,指出在 src 目录中找不到文件 App.css,但它在那里。
它通过名称识别模块
但是它只能通过路径检测文件 正如您将在 the documentation
中看到的module-name The module to import from. This is often a relative or absolute path name to the .js file containing the module. Certain bundlers may permit or require the use of the extension; check your environment. Only single quoted and double quoted Strings are allowed.
name Name of the module object that will be used as a kind of namespace when referring to the imports.
无需指定路径即可导入已安装的节点模块。
App.css不是节点模块,因此需要指定路径
自 create react app V3 发布以来,您还可以输入绝对路径,而不是有时令人困惑的方法 './../file.js'