欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

typescript配置alias的详细步骤

程序员文章站 2022-04-01 09:07:39
1 安装依赖npm install --save-dev babel-plugin-module-resolver# yarn add babel-plugin-module-resolver --d...

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也可以参考如上步骤

到此这篇关于typescript配置alias的详细步骤的文章就介绍到这了,更多相关typescript配置alias内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!