乐趣区

关于typescript:typescript-配置-alias

1 装置依赖

npm install --save-dev babel-plugin-module-resolver
# yarn add babel-plugin-module-resolver --dev

根目录新增 .babelrc 文件
参考以下内容按您我的项目中的须要去批改

{"presets": ["next/babel"],
  "plugins": [
    [
      "module-resolver",
      {
        "alias": {
          "@/actions": "./actions",
          "@/components": "./components",
          "@/constants": "./constants",
          "@/pages": "./pages",
          "@/public": "./public",
          "@/reducers": "./reducers",
          "@/utils": "./utils"
        }
      }
    ]
  ]
}

批改 tsconfig.json 文件

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {"@components/*": ["./components/*"],
      "@pages/*": ["./pages/*"],
      "@public/*": ["./public/*"]
    }
  }
}

留神 "baseUrl": "./", 不能省去,否则 ts 报 Option 'paths' cannot be used without specifying '--baseUrl' option. 谬误

next.js 中配置 alias 也能够参考如上步骤

退出移动版