使用TypeScript开发React Native应用示例教程(2)

使用TypeScript开发React Native应用示例教程

在package.json中添加一些脚本

当我们使用CRNA命令创建上面的项目时,package.json它在scaffolded项目文件夹中创建的文件包含以下脚本:

... “scripts”:{ “start”:“react-native-scripts start”, “eject”:“react-native-scripts弹出”, “Android”:“react-native-scripts android”, “ios” :“react-native-scripts ios”, “test”:“node node_modules / jest / bin / jest.js” } ...

现在,我们将添加更多脚本来运行我们的任务来编译Typescript,创建构建并启动我们的应用程序:

"lint": "tslint src/**/*.ts", "tsc": "tsc", "clean": "rimraf build", "build": "yarn run clean && yarn run tsc --", "watch": "yarn run build -- -w", "watchAndRunAndroid": "concurrently \"yarn run watch\" \"yarn run android\"", "buildRunAndroid": "yarn run build && yarn run watchAndRunAndroid ", "watchAndRunIOS": "concurrently \"yarn run watch\" \"yarn run ios\"", "buildRunIOS": "yarn run build && yarn run watchAndRunIOS ", "watchAndStart": "concurrently \"yarn run watch\" \"yarn run start\"", "buildAndStart": "yarn run build && yarn run watchAndStart " 在项目的根文件夹下添加App.js

请注意,在package.json最初由CRNA创建的文件中,app 的“main”入口点设置为:

“main”:“./ node_modules/react-native-scripts/build/bin/crna-entry.js”

意味着我们的应用程序从此crna-entry.js 文件开始。打开这个文件,你会发现它引用了我们的App.js文件:

<span>var _App = require('../../../../ App');</span>

这意味着它期望app模块位于App.js我们项目的根文件夹下的文件中。但是,我们将原始App.js文件移动到该src文件夹。此外,Typescript编译器将在该build文件夹下输出已转换的ts-to-js文件。

因此,为了让CRNA使用我们更改的文件夹结构和我们的Typescript配置,让我们App.js在项目文件夹下添加一个文件,该文件将只导出我们的App组件src/App.tsx,Typescript编译器将输出到该build文件夹。

App.js在项目的根文件夹下创建文件: import App from './build/App'; export default App; 3.运行我们的应用程序

我们现在都设置为运行我们的Typescript应用程序。运行命令:

yarn run buildAndStart

注:好了,到此,你的项目应该已经运行起来了,有任何疑问,欢迎留言。

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11984.html