npm i babel-plugin-module-resolver
这是 babel.config.js
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: [
[
"module-resolver",
{
alias: {
"@screens": "./src/screens",
"@components": "./src/components",
"@services": "./src/services",
"@infrastructure": "./src/infrastrcuture",
"@assets": "./assets",
},
},
],
],
};
};
我认为这个设置就足够了,但事实并非如此。然后是 tsconfig.json:
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"jsx": "react-native",
"lib": ["dom", "esnext"],
"moduleResolution": "node",
"noEmit": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"strict": true,
// I added this part
"baseUrl": ".",
"paths": {
"@assets": ["./assets/*"],
"@*": ["src/*"]
}
}
}
解析器适用于 src 目录,但问题在于“assets”目录。我一直在改变这条线"@assets": ["./assets/*"],
"@assets": ["assets/ "], "@assets": ["./assets/ "],
我不知道如何解决这个问题。