javascript 从父目录反应导入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33937726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React import from parent directory
提问by Maciej S
I am unable to import file from parent of project directory. To make things as simple as possible, I created new react-native project and file 'test.js' in a parent directory. I tried to import it with this code:
我无法从项目目录的父级导入文件。为了让事情尽可能简单,我在父目录中创建了新的 react-native 项目和文件“test.js”。我尝试使用以下代码导入它:
var Test = require('../test.js');
and
和
import Test from ('../test.js');
None of these worked - when run in xcode I have following error:
这些都不起作用 - 在 xcode 中运行时,我有以下错误:
uncaught error Error: UnableToResolveError: Unable to resolve module ../test.js from /Users/UserName/Downloads/TestReact/index.ios.js: Invalid directory /Users/UserName/Downloads/test.js
未捕获的错误错误:UnableToResolveError:无法从/Users/UserName/Downloads/TestReact/index.ios.js解析模块../test.js:无效目录/Users/UserName/Downloads/test.js
Is it possible to import file from parent directory with react-native? I? ?k?n?o?w? ?i?t? ?w?o?r?k?s? ?w?i?t?h? ?r?e?a?c?t?J?S?.
是否可以使用 react-native 从父目录导入文件?一世??知道??它??作品??和??r?e?a?c?t?J?S?。
Regards
问候
edit - adding code
编辑 - 添加代码
test.js
测试.js
'use strict';
import React, {Component,View,Text} from 'react-native';
class Test extends Component{
render(){
return (
<View>
<Text>
SAMPLE TEXT
</Text>
</View>
);
}
}
module.exports = Test;
index.ios.js
index.ios.js
'use strict';
import React, {AppRegistry, View} from 'react-native';
import Test from '../test.js';
var TestReact = React.createClass({
render: function() {
return (
<View><Test/></View>
);
}
});
AppRegistry.registerComponent('TestReact', () => TestReact);
edit - added files hierarchy: screenshot
编辑 - 添加的文件层次结构: 截图
edit - actually I was wrong, it's impossible with web react too. When I try to build I got following error:
编辑 - 实际上我错了,网络反应也不可能。当我尝试构建时,出现以下错误:
Module parse failed: /path_to_file/aaa1.js Line 1: Unexpected token You may need an appropriate loader to handle this file type.
模块解析失败:/path_to_file/aaa1.js 第 1 行:意外标记 您可能需要一个合适的加载程序来处理此文件类型。
So adding react tag to the question.
所以在问题中添加反应标签。
回答by AlexNikonov
Try this ./../Component.js
. It works for me.
More over if you need access a component from adjacent directory ./../AdjDir/Component.js
试试这个./../Component.js
。这个对我有用。如果您需要从相邻目录访问组件,则更多./../AdjDir/Component.js
回答by Ruben Rick
This problem may be because react only allows you to import from the src folder. So all resources have to placed in this directory.
这个问题可能是因为 react 只允许你从 src 文件夹中导入。所以所有的资源都要放在这个目录下。
Also when you go more than two parent directories back you need to use this syntax: ../../../myfolder/myFile.js enter image description here
此外,当您返回两个以上的父目录时,您需要使用以下语法:../../../myfolder/myFile.js enter image description here
回答by Narayan Shrestha
I looks fine to me . Did you tried with just
我看起来很好。你有没有试过
import Test from '../test';
instead of
代替
import Test from '../test.js';
And also try without small bracket "( )";
并且也尝试不使用小括号“( )”;
I solved my issue with this. Hope it helps.
我用这个解决了我的问题。希望能帮助到你。