react-native-template-typescript使用必知

3,726 阅读2分钟

万事开头难,学习React Native的环境搭建也是坑洼多多

当我们按照官网提供的开始搭建reactnative.cn/docs/gettin…

0. 创建项目前须知

如果之前使用过一定要执行

npm uninstall -g react-native-cli

你因为这样就完了?你太小看能作的React Native 了

1. 创建TS项目使用命令

npx react-native init MyApp  --template react-native-template-typescript

1.1 报错 Not found

error An unexpected error occurred: 
"https://registry.npmjs.org/react-native-template-react-native-template-typescript: 
Not found".

为什么?其实之前的命令并不能写在干净

1.2 你需要执行where react-native

如果此时你的电脑告诉你一个路径,说明你还需要一个操作

打开访达->command+shift+g ->输入/usr/local/bin/react-native 回车

1.3 找到react-native并删除

在次执行where react-native

再次尝试安装

npx react-native init MyApp --template react-native-template-typescript

1.4 下载正常,但是.....pod install

error Error: Failed to install CocoaPods dependencies for iOS project, 
which is required by this template.

莫慌,按着它给的提示执行

cd ./TSMobile/ios && pod install

1.5 如果没有安装成功,报错 podfile of null

说明你的CocoaPods没有安装好,

react-native 0.60 及以上版本依赖CocoaPods安装。按照这个进行调整

CocoaPods 是一个 Cocoa 和 Cocoa Touch 框架的依赖管理器,具体原理和 Homebrew 有点类似,都是
从 GitHub 下载索引,然后根据索引下载依赖的源代码。对于旧版的 CocoaPods 可以使用如下方法使用
tuna 的镜像:
pod repo remove master
pod repo add master https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git
pod repo update
新版的 CocoaPods 不允许用pod repo add直接添加master库了,但是依然可以:
cd ~/.cocoapods/repos 
pod repo remove master
git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master
最后进入自己的工程,在自己工程的 MyApp->iOS->podFile第一行加上:source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'

安装成功

点击这个文件,会自动打开Xcode

2. 点击启动,自动启动iOS,启动成功

3.安卓配置

安卓配置最容易错误的是环境变量的配置

  • 在MAC下保证 adb 命令可以执行
  • 必须在adb可以执行的地方运行,不可在vscode 终端等类似地方使用 yarn android
  • Android Studio 的sdk配置地址和执行echo $ANDROID_HOME 保持一致