freeCodeCamp/guide/chinese/react-native/basic-commands/index.md

67 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Basic Commands
localeTitle: 基本命令
---
## 基本命令
在这里您将找到使用React Native开始开发iOS和Android应用程序的基本命令列表。如果您尚未安装强烈建议您按照[官方指南进行操作](https://facebook.github.io/react-native/docs/getting-started.html) 。
### 开始一个新项目
您可以通过不同的方式引导反应本机应用程序。您可以使用**Expo**或`create-react-native-app` 它们依次使用Expo-Cli来启动您的新项目但是通过这种方法您可以更好地控制项目中发生的事情并可以进行沟通调整和编写拥有适用于iOS和Android移动平台的本机库的模块。
```
react-native init [PROJECT-NAME]
cd [PROJECT-NAME]
```
**在Android模拟器中运行应用**
这个命令是自我解释的因为它说它将启动Android模拟器并安装刚刚创建的应用程序。您需要位于项目的根目录中才能运行此命令。
```
react-native run-android
```
**在iOS模拟器中运行应用程序**
此命令与`react-native run-android`完全相同但它不是Android模拟器而是打开iPhone模拟器。
```
react-native run-ios
```
**将依赖项链接到本机项目**
某些库具有需要在为React Native生成的本机代码中链接的依赖项。如果在安装新库后某些操作无效可能是因为您跳过此步骤。
```
react-native link [LIBRARY-NAME]
```
**清除捆绑**
如果某些内容未按预期运行则可能需要使用此命令清除并创建新的bundle。
```
watchman watch-del-all
```
**支持装饰者**
JSX默认不支持装饰器因此您需要安装**Babel**插件才能使其工作。
```
npm install babel-plugin-transform-decorators-legacy --save
npm install babel-plugin-transform-class-properties --save
```
### 导出APK以在设备上运行
使用以下命令您将拥有和未签名的apk以便您可以安装并与同事共享以进行测试。请记住这个apk还没有准备好上传到App Store或制作。 你会发现你的新鲜apk在_android / app / build / outputs / apk / app-debug.apk_
**1.捆绑调试版本**
```
react-native bundle --dev false --platform android --entry-file index.android.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug
```
**2.创建调试版本**
```
cd android
./gradlew assembleDebug
```