react native中想要用过‘@/…’的写法写绝对路径需要第三方插件:babel-plugin-module-resolver
1、安装插件
yarn add babel-plugin-module-resolver
2、配置babel.config.js
plugin: [
'module-resolver',
{
root: ['./src'], // 从哪个文件开始设置绝对路径
alias: {
'@/utils': './src/utils', // 别名
},
},
],
3、在tsconfig.json中加入一下节点
"baseUrl": "./src",
"paths": {
"@/assets/*": ["assets/*"],
"@/components/*": ["components/*"],
"@/config/*": ["config/*"],
"@/models/*": ["models/*"],
"@/navigator/*": ["navigator/*"],
"@/pages/*": ["pages/*"],
"@/utils/*": ["utils/*"]
},