怎么在ReactNative里面使用Typescript
2018-11-29 20:07
671 查看
今天来搞一搞怎么搭建一个可以使用Typescript的ReactNative环境好吧,一句废话不多说,直接开始好吧
1.全局安装create-react-native-app
yarn global add create-react-native-app
2.create-react-native-app 你的项目名称
例如:create-react-native-app myApp
运行完选择blank回车等待就好
3.cd到你的项目文件夹中,准备安装typeScript依赖
4.安装typeScript依赖
yarn add typescript tslint -D yarn add @types/react @types/react-native @types/react-dom -D
5.继续安装其他依赖
yarn add concurrently rimraf -D
6.通过tsc --init生成tsconfig.json,或者手动创建一个tsconfig.json,将下面代码拷入该文件
{ "compilerOptions": { "module":"es2015", "target": "es2015", "jsx": "react", "rootDir": "src", "outDir": "build", "allowSyntheticDefaultImports": true, "noImplicitAny": true, "sourceMap": true, "experimentalDecorators": true, "preserveConstEnums": true, "allowJs": true, "noUnusedLocals": true, "noUnusedParameters": true, "noImplicitReturns": true, "skipLibCheck": true, "moduleResolution":"Node" }, "filesGlob": [ "typings/index.d.ts", "src/**/*.ts", "src/**/*.tsx", "node_modules/typescript/lib/lib.es6.d.ts" ], "types": [ "react", "react-dom", "react-native" ], "exclude":[ "build", "node_modules", "jest.config.js", "App.js" ], "compileOnSave": false }
7.安装react-native-scripts
yarn add react-native-scripts
8.将package.json中的"scripts"配置清空,并将以下代码替换
"scripts": { "start": "react-native-scripts start", "eject": "react-native-scripts eject", "android": "react-native-scripts android", "ios": "react-native-scripts ios", "test": "node node_modules/jest/bin/jest.js", "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 " }
9.将package.json中的"main"配置清空替换为以下代码
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js"
10.将App.js中代码清空,并将以下代码替换
import App from './build/App'; export default App;
11.创建一个src文件夹,将babel.config.js文件放在src文件夹下,同时在src文件夹下创建一个App.tsx文件,App.tsx文件中代码如下
import React, { Component } from "react" import { View, Text } from "react-native" export default class App extends Component { render() { return( <View> <Text>红牛维生素功能饮料</Text> </View> ) } }
12.执行yarn buildAndStart即可
注意在此环境下编写ts文件时,后缀名写为tsx,暂时不知道什么原因,写ts后缀名有问题
还有特别注意一下,在此环境导入图片时,用import有问题,只能用require,这个问题暂时不知道原因
相关文章推荐
- android 中 webview 怎么用 localStorage? 我在 android里面 使用html5的 localStorage 为什么存不进去也读不出来呀? 网上搜了好多都没效果
- svn问题:在eclipse里面使用SVN,怎么实现版本回滚呢?
- 你们帮我看看如何在datalist里面使用session获取ID值代码改怎么写啊麻烦了
- android里面怎么使用mina框架与mina服务器连接之传对象
- truts1里面的logic:equal标签该怎么使用
- 32位代码段不能写显存吗?==里面提到怎么在汇编使用16位,32位标志
- CSS里面的behavior属性的作用是什么,怎么使用
- mybatis 里面使用<if test=""> 判断,当后台传的值是数字字符串时,怎么处理
- Linux 怎么把自己写的脚本添加到服务里面,即可以使用service命令来调用
- oracle中nvl函数在mysql里面怎么使用
- 使用J-link下载程序的时候,程序下载到里面,只要下载器拔了程序就不运行了,这是怎么回事?
- 我有一个免费空间,里面有PHP,MYSQL但不知道怎么使用
- asp.net 里面怎么使用 include?
- js里面的for in 怎么使用.md
- Android使用SimpleAdapter更新ListView里面的Drawable元素
- Typescript 入门 及 使用Visual Studio Code搭建TypeScript开发环境
- 如果你参与到一个项目中,发现他们使用 Tab 来缩进代码,但是你喜欢空格,你会怎么做?
- 微信公众号怎么做动画页面-微信公众号使用教程17
- Android studio使用过程中提示过时操作怎么办?
- 使用python3的asyncio写的网页不显示,会直接变成下载文件,怎么办?