删除 react-native-cli
npx react-native init demo --template react-native-template-typescript
绝对路径 下载插件 babel-plugin-module-resolver
配置 babel.config.js
1 module.exports = { 2 presets: ['module:metro-react-native-babel-preset'], 3 plugins:[ 4 [ 5 'module-resolver', 6 { 7 root:['./src'],//从那个路径配置 8 alias:{ 9 '@/utils':'./src/utils',//映射 10 "@/pages":"./src/pages", 11 "@/navigator":"./src/navigator", 12 "@/assets":"./scr/assets", 13 "@/config":"./src/config", 14 "@/components":"./src/components" 15 } 16 } 17 ] 18 ] 19 };
由于基于ts
tsconfig.json配置
1 "baseUrl": "./src", /* Base directory to resolve non-absolute module names. */ 2 "paths": { 3 "@/assets/*":["assets/*"], 4 "@/components/*":["components/*"], 5 "@/config/*":["config/*"], 6 "@/models/*":["models/*"], 7 "@/navigator/*":["navigator/*"], 8 "@/utils/*":["utils/*"], 9 "@/pages/*":["pages/*"] 10 },