React + Webpack + TypeScript 配置别名

72 阅读1分钟
  1. 暴露出 webpack.config.js :运行命令 yarn eject
  2. 修改 webpack.config.js 与 ts.config.json
  • webpack.config.js alias
alias{
        // 文件路径别名
        '@': path.resolve(__dirname, '../src'),
        '@component': path.resolve(__dirname, '../src/component'),
        '@util': path.resolve(__dirname, '../src/util'),
        '@router': path.resolve(__dirname, '../src/router'),
        '@store': path.resolve(__dirname, '../src/store'),
        '@interface': path.resolve(__dirname, '../src/interface'),
        }
  • ts.config.js
      "paths": {
      "@/*": ["src/*"],
      "@component/*": ["src/component/*"],
      "@util/*": ["src/util/*"],
      "@router/*": ["src/router/*"],
      "@store/*": ["src/store/*"],
      "@interface/*": ["src/interface/*"]
    },
  1. WebStorm 设置 webpack.config.js 位置