解决vscode 开发react 导入绝对路径 无法跳转的问题

相对路径可正常跳转

import { namespace, getStaffs as actionGetStaffs, addStaffs as actionAddStaffs } from '../../redux/actions/staff'

但是在webpack配置alias使用绝对路径后无法跳转

webpack配置:

resolve: {
       modules: [path.join(__dirname, 'src'), 'node_modules'],
       extensions: ['.js', '.jsx', '.json', '.scss', '.css'],
       alias: {
            src: path.join(__dirname, 'src'),
       },
       plugins: [
            new ModuleScopePlugin(appSrc, [appPackageJson]),
       ],
},

// 文件导入

import { namespace, getStaffs as actionGetStaffs, addStaffs as actionAddStaffs } from 'src/redux/actions/staff'

解决办法:
需要添加一个jsconfig文件,如下:

{
  "compilerOptions": {
    "baseUrl": "./",  // all paths are relative to the baseUrl
    "paths": {
        "src/*" : ["src/*"]   
    }
  }
}

参考地址:

    原文作者:JayChang2017
    原文地址: https://segmentfault.com/a/1190000018597397
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞