初始化项目
1 | npx react-native init projectname --template react-native-template-typescript |
多环境
1 | yarn add react-native-config |
根目录下新建 .env
1
2API_URL=https://myapi.com
GOOGLE_MAPS_API_KEY=abcdefgh
使用1
2
3
4import Config from "react-native-config";
Config.API_URL; // 'https://myapi.com'
Config.GOOGLE_MAPS_API_KEY; // 'abcdefgh'
安装
ios1
(cd ios; pod install)
android
android/app/build.gradle1
2// 2nd line, add a new apply:
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
绝对路径
1 | yarn add babel-plugin-module-resolver |
配置 .babelrc
或 babel.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
[
'module-resolver',
{
root: ['./src'],
alias: {
'@': './src',
},
},
],
],
};
配置 tsconfig.json
1 | { |