我试图遵循这一中级教程:
https://medium.com/@bryantheastronaut/react-getting-started-the-mern-stack-tutorial-feat-es6-de1a2886be50
其他试图遵循这一点的人评论说,他们与我有相同的问题,尽管该教程中尚未找到解决此问题的方法,类似的问题也与图像文件有关,其中的答案是将特定文件移动到src目录。
问题是引用数据。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帖子也有类似的问题:
The create-react-app imports restriction outside of src directory
但是,解决方案是将图像文件移动到src目录。
有人知道如何链接到react文件中与src目录处于同一级别的文件吗?我不明白node\u modules目录中的符号链接是什么意思,也找不到如何制作符号链接的教程。