无法连接本地本地主机 [英] Can't connect react native to localhost

查看:75
本文介绍了无法连接本地本地主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 iOS 应用程序中,包括 React 库及其依赖项,我在启动时有以下代码:

In my iOS app, which includes the React library and its dependencies, I have the following code on startup:

NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle?platform=ios"];RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation模块名称:@"应用程序"初始属性:@{}启动选项:@{}];rootView.frame = self.view.bounds;[self.view addSubview:rootView];

当我在模拟器中运行它时,即使在禁用 ATS 并运行 npm start 之后,我得到:

When I run this in the simulator, even after disabling ATS and running npm start, I get:

无法加载包(http://localhost:8081/index.bundle?platform=ios),错误:(无法连接到开发服务器.确保以下几点:- 节点服务器正在运行并在同一网络上可用 - 从 react-native root 运行npm start"- 在 AppDelegate 中正确设置了节点服务器 URL- WiFi 已启用并连接到与节点服务器相同的网络

我的 package.json 文件是这样的:

My package.json file is this:

<代码>{"name": "AwesomeProject",版本":0.1.0",私人":真的,开发依赖":{"react-native-scripts": "1.11.1","jest-expo": "25.0.0",反应测试渲染器":16.2.0"},"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",脚本":{"start": "react-native-scripts 开始","eject": "react-native-scripts 弹出","android": "react-native-scripts android","ios": "react-native-scripts ios","test": "节点 node_modules/jest/bin/jest.js"},开玩笑":{预设":笑话博览会"},依赖关系":{"博览会": "^25.0.0",反应":16.2.0",反应原生":0.52.0",反应导航":^1.5.11"}}

另一件事...似乎 npm start 导致端口 19000,而不是 8081 打开,但是当我转到 http://localhost:19000 并使用他们在该 JSON 中提供的捆绑 URL,它告诉我我需要使用 Expo.但是 https://facebook.github.io/react-native/docs/integration-with-existing-apps.html,我不知道从哪里开始.有什么想法吗?

Another thing... it seems that npm start causes port 19000, not 8081, to open, but when I go to http://localhost:19000 and use the bundle URL that they provide in that JSON, it tells me that I need to use Expo. But none of that is mentioned in https://facebook.github.io/react-native/docs/integration-with-existing-apps.html, and I'm not sure where to go from here. Any ideas?

推荐答案

如果你想将 react-native 引入你现有的原生 iOS 项目,那么这里不需要 expo 包,并且启动脚本应该是node node_modules/react-native/local-cli/cli.js start.

If you want to bring react-native to your existing native iOS project, then expo package isn't expected here, and the start script should be node node_modules/react-native/local-cli/cli.js start.

只需关注与现有应用程序集成 指南,你只需要 react-nativereact 包.(以及 react-navigation,如果您的应用将其用于导航).

Just follow Integration with Existing Apps guide, you only need react-native and react package. (and react-navigation if your app use that for navigation).

这篇关于无法连接本地本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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