相关疑难解决方法(0)

如何在基于 TypeScript 的 React Native 应用程序中为导入配置绝对路径?

为了避免在基于 TypeScript 的 React Native 应用程序中出现“../../../../”样式的相对导入,我想配置该应用程序,以便我可以使用绝对导入。

重要的是配置还支持 Jest 单元测试。

我创建的应用程序使用 npx react-native init MyTestApp --template typescript

反应本机版本:0.60.5

我需要实现这一目标的确切配置是什么?

javascript typescript jestjs babeljs react-native

26
推荐指数
5
解决办法
2万
查看次数

带有react、react-app-rewire 和 typescript 的绝对路径

我正在使用create-react-app+ typescript,并且我想添加绝对路径。

我试图达到可以使用绝对路径的程度,如下所示:

代替import x from '../../../components/shell/shell'

使用import x from '@components/shell/shell';

这是tsconfig.json文件:

{
  "extends": "./paths.json",
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "baseUrl": "src",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

Run Code Online (Sandbox Code Playgroud)

我使用扩展文件作为路径,因为由于某种原因npm start覆盖了该文件。paths.json文件也是如此:

{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@": ["./src"],
      "@components/*": …
Run Code Online (Sandbox Code Playgroud)

reactjs webpack

4
推荐指数
1
解决办法
9394
查看次数