ReactNative typescript路径别名配置,vscode不识别@/youpath配置方法


使用“@/yourpath” 代替 “../../youpath”的写法 ,需要安装插件 babel-plugin-root-import

1.使用npm 或 yarn 安装  babel-plugin-root-import

npm install babel-plugin-root-import --save-dev
or
yarn add babel-plugin-root-import --dev

2.使用babel 配置 babel-plugin-root-import

使用.babelrc 或 babel.conf.js 两者只要配一个!

a.使用.babelrc 

{
  "plugins": [
    ["babel-plugin-root-import"], {
       rootPathSuffix: "./src",
       rootPathPrefix: "@/"
     }
  ]
}

b.使用babel.conf.js

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ["babel-preset-expo", "mobx"],
    plugins: [
      [
        "babel-plugin-root-import",
        {
          rootPathSuffix: "./src",
          rootPathPrefix: "@/"
        }
      ]
    ]
  };
};

 

 

vscode 不识别“@/youpath”解决:需要在tsconfig.json 添加以下配置

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  }
}

 

 

参考文档:

https://www.npmjs.com/package/babel-plugin-root-import   babel-plugin-root-import 插件文档

https://www.jianshu.com/p/6d2783f1ab51  tsconfig.json 配置列表


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM