簡體   English   中英

React-從外部src目錄導入文件

[英]React - importing files from outside src directory

我正在嘗試遵循此中級教程: 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帖子有一個類似的問題: create-react-app導入限制在src目錄之外 ,但是解決方案是將映像文件移動到src目錄。

有沒有人想出如何鏈接到與React文件中src目錄相同級別的文件? 我不了解node_modules目錄中的符號鏈接的含義,也找不到如何制作符號鏈接的教程。

我建議您僅將data.js文件放在src/目錄中。 只是模擬數據,對不對? 對於您的開發,它應該是src一部分。 只需完成並完成本教程即可。 就這樣。 如果您確實在使用生產應用程序,則可以退出該應用程序以在整個配置上具有更大的靈活性,從而可以消除該限制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM