金沙澳门官网登录-www.js8.com-金沙澳门官网下载app

使用Visual Studio Code和typescript 开发调试React Nativ

使用Visual Studio Code和typescript 开发调试React Native项目。关于React Native的详实介绍本身就不陈说了,他是应用js营造原生app的付出框架。一遍变编码多平台运营,特别强盛。不过个人嫌恶js的过于灵活(弱类型)的语法。强盛的强类型语言Typescript(简称TS)是笔者的首荐,他得以编写翻译成JavaScript,编写翻译成的JavaScript代码可读性很好,可是那不是最重要,关键是TS支出和调弄收拾效用非常高。
但是React Native官方是选择js使用Visual Studio Code和typescript 开发调试React Native项目。使用Visual Studio Code和typescript 开发调试React Native项目。的支出的,假设黄金年代旦利用TS开垦React Native的首假使transformer
使用Visual Studio Code和typescript 开发调试React Native项目。eact-native结合的重假设利用转变器

初阶化项目

react-native init YahuiApp
cd YahuiApp
yarn add --dev react-native-typescript-transformer typescript @types/react @types/react-native

用vscode张开 增多配置文件

配置Typescript

新建文件 tsconfig.json内容为

{
    "compilerOptions": {
        "module": "es2015",
        "target": "es2015",
        "moduleResolution": "node",
        "jsx": "react-native",
        "noImplicitAny": true,
        "experimentalDecorators": true,
        "preserveConstEnums": true,
        "sourceMap": true,
        "watch": true,
        "allowSyntheticDefaultImports": true
    },
    "filesGlob": [
        "src/**/*.ts",
        "src/**/*.tsx"
    ],
    "exclude": [
        "index.android.js",
        "index.ios.js",
        "build",
        "node_modules"
    ]
}

新建文件 tslint.json 内容为

{
    "rules": {
        "class-name": false,
        "comment-format": [
            true,
            "check-space"
        ],
        "indent": [
            true,
            "spaces"
        ],
        "no-duplicate-variable": true,
        "no-eval": true,
        "no-internal-module": true,
        "no-trailing-whitespace": true,
        "no-unsafe-finally": true,
        "no-var-keyword": true,
        "one-line": [
            true,
            "check-open-brace",
            "check-whitespace"
        ],
        "quotemark": [
            true,
            "double"
        ],
        "semicolon": [
            true,
            "always"
        ],
        "triple-equals": [
            true,
            "allow-null-check"
        ],
        "typedef-whitespace": [
            true,
            {
                "call-signature": "nospace",
                "index-signature": "nospace",
                "parameter": "nospace",
                "property-declaration": "nospace",
                "variable-declaration": "nospace"
            }
        ],
        "variable-name": [
            true,
            "ban-keywords"
        ],
        "whitespace": [
            true,
            "check-branch",
            "check-decl",
            "check-operator",
            "check-separator",
            "check-type"
        ]
    }
}

配置React Native Packager

根目录新建rn-cli.config.js文件 内容为:
module.exports = {
getTransformModulePath() {
return require.resolve('react-native-typescript-transformer');
},
getSourceExts() {
return [ 'ts', 'tsx' ]
}
};

编辑代码

在 src文件夹里新建main.tsc文件
代码为:

import React, { Component } from "react";
import {
    StyleSheet,
    Text,
    View
} from "react-native";
interface Props {

}
interface State {

}
export default class App extends Component<Props, State> {
    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.text}>
                    Welcome to React Native!
                </Text>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: "center",
        alignItems: "center",
        backgroundColor: "#F5FCFF",
    } as React.ViewStyle,

    text: {
        fontSize: 20,
        textAlign: "center",
        margin: 10,
    } as React.TextStyle,
});

AppRegistry

import {
    AppRegistry,
} from 'react-native';
import App from "./src/main";

AppRegistry.registerComponent('YahuiApp', () => App);

时至前几日 您的行使TS开拓React Native的种类景况搭建好了

本文由金沙澳门官网登录发布于www.js8.com,转载请注明出处:使用Visual Studio Code和typescript 开发调试React Nativ

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。