React - 从外部 src 目录导入文件
React - importing files from outside src directory
我正在尝试跟随这个 Medium 教程:https://medium.com/@bryantheastronaut/react-getting-started-the-mern-stack-tutorial-feat-es6-de1a2886be50
其他尝试过此操作的人评论说他们遇到了与我相同的问题,尽管该问题的解决方案尚未在该教程中找到,并且 SO 上的类似问题与图像文件有关,答案在哪里就是把具体的文件移动到src目录下。
问题是引用了位于顶层的 data.js 文件(与 src 目录同一层。
当我尝试将其合并到文件中时,如下所示:
/CommentBox.js
import React, { Component } from 'react';
import CommentList from './CommentList';
import CommentForm from './CommentForm';
import DATA from '../data';
import style from './style';
class CommentBox extends Component {
constructor(props) {
super(props);
this.state = { data: [] };
}
render() {
return (
<div style={ style.commentBox }>
<h2>Comments:</h2>
<CommentList data={ DATA }/>
<CommentForm />
</div>
)
}
}
export default CommentBox;
出现以下结果的错误:
Failed to compile.
./src/CommentBox.js
Module not found: You attempted to import ../data which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project's node_modules/.
这个 SO post 有一个类似的问题:,但是解决方案是将图像文件移动到 src 目录。
有没有人想出如何 link 到与 react 文件中的 src 目录同一级别的文件?我不明白 node_modules 目录中的 symlink 是什么意思,我也找不到制作教程。
我建议您只需将 data.js
文件放入 src/
目录即可。这只是模拟数据,对吧?对于您的开发,它应该是 src
的一部分。只需这样做并完成教程即可。就这样。如果您真的在处理生产应用程序,您会弹出该应用程序以在其整个配置上具有更大的灵活性,从而能够消除该限制。
我正在尝试跟随这个 Medium 教程:https://medium.com/@bryantheastronaut/react-getting-started-the-mern-stack-tutorial-feat-es6-de1a2886be50
其他尝试过此操作的人评论说他们遇到了与我相同的问题,尽管该问题的解决方案尚未在该教程中找到,并且 SO 上的类似问题与图像文件有关,答案在哪里就是把具体的文件移动到src目录下。
问题是引用了位于顶层的 data.js 文件(与 src 目录同一层。
当我尝试将其合并到文件中时,如下所示:
/CommentBox.js
import React, { Component } from 'react';
import CommentList from './CommentList';
import CommentForm from './CommentForm';
import DATA from '../data';
import style from './style';
class CommentBox extends Component {
constructor(props) {
super(props);
this.state = { data: [] };
}
render() {
return (
<div style={ style.commentBox }>
<h2>Comments:</h2>
<CommentList data={ DATA }/>
<CommentForm />
</div>
)
}
}
export default CommentBox;
出现以下结果的错误:
Failed to compile.
./src/CommentBox.js
Module not found: You attempted to import ../data which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project's node_modules/.
这个 SO post 有一个类似的问题:
有没有人想出如何 link 到与 react 文件中的 src 目录同一级别的文件?我不明白 node_modules 目录中的 symlink 是什么意思,我也找不到制作教程。
我建议您只需将 data.js
文件放入 src/
目录即可。这只是模拟数据,对吧?对于您的开发,它应该是 src
的一部分。只需这样做并完成教程即可。就这样。如果您真的在处理生产应用程序,您会弹出该应用程序以在其整个配置上具有更大的灵活性,从而能够消除该限制。