在ReactNative中使用Typescript
在ReactNative中使用Typescript
少侠放心,跟着我的这个步骤走,保你完美在RN项目中使用Typescript,废话不多说,走你
1.全局安装create-react-native-app
yarn global add create-react-native-app
2.创建项目
create-react-native-app projectname(你的项目名字)
3.cd到你的项目文件夹中
4.安装typescript依赖
yarn add typescript tslint -D
yarn add @types/react @types/react-native @types/react-dom -D
5.安装其他依赖
yarn add concurrently rimraf -D
yarn add ts-jest @types/jest @types/react-test-renderer -D
6.在你的项目根目录下创建一个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>不成功加我qq:291678978,手把手教学好吧</Text>
</View>
)
}
}
12.执行命令:yarn buildAndStart,然后静静的等待运行成功,用你伟大的expo扫描成功的二维码就可以看到伟大的胜利;注:如果想在浏览器看到你的二维码,可再单独执行一下yarn start
然后就可以很开心的在项目里写TypeScript代码了,例如项目中tsx目录下有test.tsx文件,我们在import这个文件时,就像import一个js文件就可以了(注:ts文件后缀名ts和tsx都可以,不过在当前环境下后缀名写成ts好像有问题,如果有问题的话将后缀名改成tsx即可,亲测有效)
import './tsx/test'
在ReactNative中使用Typescript的更多相关文章
- webpack中使用typescript
概述 这是我学习webpack中使用typescript的记录,供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看中文文档或英文文档,休闲之余可以看这篇TypeScript 总 ...
- Angular 个人深究(一)【Angular中的Typescript 装饰器】
Angular 个人深究[Angular中的Typescript 装饰器] 最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码. 注:本人前端小白一枚,文章旨在记录自己 ...
- 在react-native中使用es7语法中的decorator装饰器
在react-native中默认使用decorator会红屏报错,需要安装一个babel插件: babel-plugin-transform-decorators-legacy 然后在根目录下的.ba ...
- 在visual studio code和visual studio中编写TypeScript文件自动生成JavaScript文件
注:此处的自动生成都为保存ts文件时自动生成js文件 VS CODE 只需要在TypeScript的终端控制台中输入如下命令即可,并注意需要将其中的*换成对应的文件名,此处的*似乎不能作为通用匹配. ...
- 在Vue 中使用Typescript
Vue 中使用 typescript 什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类 ...
- Vue 中使用 typescript
Vue 中使用 typescript 什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类 ...
- 深入浅出TypeScript(5)- 在React项目中使用TypeScript
前言 在第二小节中,我们讨论了利用TypeScript创建Web项目的实现,在本下节,我们讨论一下如何结合React创建一个具备TypeScript类型的应用项目. 准备 Webpack配置在第二小节 ...
- 如何在Vue项目中使用Typescript
0.前言 本快速入门指南将会教你如何在Vue项目中使用TypeScript进行开发.本指南非常灵活,它可以将TypeScript集成到现有的Vue项目中任何一个阶段. 1.初始化项目 首先,创建一个新 ...
- [RN] React-Native中Array渲染的优化
React-Native中Array渲染的优化 例如用Push加进去的数据: constructor(props){ super(props); this.state = { b ...
随机推荐
- Android 自定义Dialog中加EditText弹不出键盘跟Dialog遮挡键盘的问题
先上两张图 第一张问题很明显,第二张是成功的图, 其实第一张是加了 //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INP ...
- ImageLoader简单使用
效果图就是一个从网络加载的图片:在加载的时候图片的中间显示一个进度条 数据是随便找的一个网络图片的地址 导入jar包universal-image-loader-1.9.5 用来展示商品使用 在 ...
- Jmeter(一)http接口添加header和cookie --转载
Jmeter(一)http接口添加header和cookie HTTP信息头管理器在Jmeter的使用过程中起着很重要的作用,通常我们在通过Jmeter向服务器发送http请求(get或者post ...
- 30分钟LINQ教程 【转载】
原文地址:http://www.cnblogs.com/liulun/archive/2013/02/26/2909985.html 在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有 ...
- 1.windows下GIT 服务安装
本章介绍简单在windows 安装git 服务方法.服务器端采用的是Bonobo Git Server,一款用ASP.NET MVC开发的Git源代码管理工具,界面简洁,基于Web方式配置,简单易用. ...
- 查看pip install安装的python包的位置
例如,我在一个名为tf_14的vertualenv环境中(no-site-package)安装了一个contextlib2包 (tf_14) novak@novak-ZBook15G2:~/Carnd ...
- June 17th 2017 Week 24th Saturday
Absence sharpens love, presence strengthens it. 相聚爱益切,离别情更深. There is almost no such love that can i ...
- LA 4327 多段图
题目链接:https://vjudge.net/contest/164840#problem/B 题意: 从南往北走,横向的时间不能超过 c: 横向路上有权值,求权值最大: 分析: n<=100 ...
- iText生成PDF 格式报表
1.导包 <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artif ...
- Zookeep启动异常:Error contacting service. It is probably not running.
异常提示: [root@hadoop bin]# ./zkServer.sh status JMX enabled by default Using config: /usr/local/zk/bin ...