在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 ...
随机推荐
- JSP数据库综合练习
一.问题概述 实现了数据库的增删改查和分页显示. 分页显示:mysql String cmd = "select * from t_user limit &q ...
- 02_ActiveMQ入门
[ActiveMQ 入门HelloWorld例子] [启动ActiveMQ] 1.由于本人PC是64位的,选择在bin目录下的win64/activemq.bat启动. 2.启动成功后,访问http: ...
- winform DataGridView 通用初始化
void DGV_Init() { //名称 类型 设备数 累计转发次数 累计转发数据数 状态 ; i < ; i++) { DataGridViewTextBoxColumn dc = new ...
- Java Hotspot client模式和server模式的区别
当虚拟机运行在-client模式的时候,使用的是一个代号为C1的轻量级编译器, 而-server模式启动的虚拟机采用相对重量级,代号为C2的编译器. C2比C1编译器编译的相对彻底,服务起来之后,性能 ...
- Full scan vs index 执行计划的实验
根据Oracle-L邮件列表里主题「 Full scan vs index 」的讨论而来. 1.测试环境创建 SYS@HEMESRHTDB2(1.206)> select * from v$ve ...
- python核心编程中网络爬虫的例子
#!/usr/bin/env python import cStringIO # import formatter # from htmllib import HTMLParser # We use ...
- OC extern和函数
#include <stdio.h> // 定义一个one函数 // 完整地定义一个外部函数需要extern关键字 //extern void one() { // printf(&quo ...
- LA 4043 最优匹配
题目链接:https://vjudge.net/contest/161820#problem/A 题意: n 个 白点,n 个黑点,给出了坐标,求完美匹配后,各点不相交,输出白点对于的黑点编号:(输出 ...
- HDU 1575 Tr A 【矩阵经典2 矩阵快速幂入门】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1575 Tr A Time Limit: 1000/1000 MS (Java/Others) Me ...
- 【luogu P1514 引水入城】 题解
题目链接:https://www.luogu.org/problemnew/show/P1514 // luogu-judger-enable-o2 #include <iostream> ...