我正在嘗試在 React Native 中包含我的專案的路徑別名。事實上,當我啟動應用程式時它正在作業,所以我認為我babel.config.js的還可以,但是在 VSCode 中仍然出現一個錯誤,Cannot find module or its corresponding type declarations所以我認為我tsconfig.json錯了
babel.config.js
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
[
'module-resolver',
{
root: ['.'],
alias: {
'@ali/assets': './src/assets',
'@ali/components': './src/components',
'@ali/colors': './src/theme/colors.ts'
}
}
]
]
}
tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"lib": ["es2017"],
"allowJs": true,
"jsx": "react-native",
"noEmit": true,
"isolatedModules": true,
"strict": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"skipLibCheck": false,
"rootDir": ".",
"paths": {
"@ali/assets": ["./src/assets/*"],
"@ali/components": ["./src/components/*"],
"@ali/colors": ["./src/theme/colors"]
}
},
"exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"],
"extends": "expo/tsconfig.base"
}
這是我的檔案 src/screens/Login/index.tsx
import Wellcome from '@ali/components/LogIn/Wellcome'
import React from 'react'
import LoginForm from '../../components/forms/LoginForm'
export const LogIn = () => {
return (
<>
<Wellcome />
<LoginForm />
</>
)
}
export default LogIn
我可以在我的應用程式中看到我的組件 Wellcome,但在 VSCode 仍然出現錯誤。
uj5u.com熱心網友回復:
這是由于tsconfig.json配置不當造成的。
您應該添加baseUrl和洗掉開始的“。” 從路徑路線。
嘗試這個:
"baseUrl": ".",
"paths": {
"@ali/assets/*": ["src/assets/*"],
"@ali/components/*": ["src/components/*"],
"@ali/colors/*": ["src/theme/colors"]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/508274.html
