React-Native的本地require()路径 [英] Local require() paths for React-Native

查看:631
本文介绍了React-Native的本地require()路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方便的方法来访问应用程序根目录中的文件,同时避免使用如下所示的require()字符串:

I am looking for a convenient way to access files in the root of my application while avoiding require() strings that look like:

require('../../../../myModule')

Node有一些很好的解决方案( https://gist.github.com/branneman/8048520 )但我还没有看到在React Native中使用全局变量的方法。

There are some good solutions out there for Node (https://gist.github.com/branneman/8048520) but I haven't seen a way to use global variables in React Native.

有没有人有这个问题的干净解决方案?

Does anyone have a clean solution to this problem?

推荐答案

来自Marc Shilling关于 https的回答: //github.com/facebook/react-native/issues/3099

From Marc Shilling's answer on https://github.com/facebook/react-native/issues/3099


您可以在导入上使用绝对路径/要求:

You can use an absolute path on imports/requires:

从'MyApp / src / components'导入{ProximaNovaText};
require('MyApp / src / utils / moment-twitter');

其中'MyApp'是您注册的任何名称在你的index.ios.js文件中

where 'MyApp' is whatever name you registered in your index.ios.js file

注意VS代码:这有效,但要注意你可能会丢失intellisense和cmd /按住ctrl +单击。感谢Johan提供有关CS代码的信息

Note for VS Code: This works, but be warned that you might lose intellisense and cmd/ctrl + click. Thanks Johan for the info about CS code

这篇关于React-Native的本地require()路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆