admin管理员组文章数量:1405393
Hello i am just learning ReactJs, I am trying to import a module from a sub folder in react, here is my folder structure
-src
---ponents
-----layout
-------Header.js
-------Navigation.js
-----fakeAuth.js
From the Header.js module, i am trying to import the fakeAuth from the parent (ponent), but it seems it can't call module or am i just missing something?
I already tried the following
import fakeAuth from './fakeAuth'
import fakeAuth from '././fakeAuth'
import fakeAuth from '../../fakeAuth'
Still no luck, i know this will be easy for some. Thanks
here i my fakeAuth.js, which is from the react-router-dom tutorial.
module.exports = {
isAuthenticated: false,
authenticate(cb) {
this.isAuthenticated = true;
setTimeout(cb, 100); // fake async
},
signout(cb) {
this.isAuthenticated = false;
setTimeout(cb, 100);
}
};
Hello i am just learning ReactJs, I am trying to import a module from a sub folder in react, here is my folder structure
-src
---ponents
-----layout
-------Header.js
-------Navigation.js
-----fakeAuth.js
From the Header.js module, i am trying to import the fakeAuth from the parent (ponent), but it seems it can't call module or am i just missing something?
I already tried the following
import fakeAuth from './fakeAuth'
import fakeAuth from '././fakeAuth'
import fakeAuth from '../../fakeAuth'
Still no luck, i know this will be easy for some. Thanks
here i my fakeAuth.js, which is from the react-router-dom tutorial.
module.exports = {
isAuthenticated: false,
authenticate(cb) {
this.isAuthenticated = true;
setTimeout(cb, 100); // fake async
},
signout(cb) {
this.isAuthenticated = false;
setTimeout(cb, 100);
}
};
Share
Improve this question
edited Feb 18, 2018 at 12:59
Wassapaks
asked Feb 18, 2018 at 12:48
WassapaksWassapaks
4354 silver badges14 bronze badges
8
- 1 Module not found: You attempted to import ../../../fakeAuth.js which falls outside of the project src/ directory. – Wassapaks Commented Feb 18, 2018 at 12:49
- can you post what you have in fakeauth.js? Did you export something there? – G_S Commented Feb 18, 2018 at 12:50
- Edit: since I saw your edit now, it seems that the import would be '../faceAuth'` – Pedro Lopes Commented Feb 18, 2018 at 12:51
- sorry i edited the location of the fakeAuth.js with the same path as the layout directory. I can call the fakeAuth module when on the same directory or from a partent, but i cant call it from the sub directory. – Wassapaks Commented Feb 18, 2018 at 12:52
- 1 In your scenario wrt folder structure, ../faceAuth should work. There should be something about your faceauth – G_S Commented Feb 18, 2018 at 12:58
2 Answers
Reset to default 4It should be import fakeAuth from '../fakeAuth'
You just have to go 1 folder up where you have fakeAuth.js file. adding '..' does that.
Since you're using module.exports
you can import in the following fashion inside Header.js
:
import { isAuthenticated, authenticate, signout } from "../fakeAuth";
CodeSandbox Demo
本文标签: javascriptimport module from a sub folder in react jsStack Overflow
版权声明:本文标题:javascript - import module from a sub folder in react js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744265121a2597911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论