react+ts配置别名alias

1.下载craco

yarn add @craco/craco 

2.创建一个craco.config.js文件

const path = require('path');
module.exports = {
  webpack: {
    alias: {
      '@': path.join(__dirname, 'src'),
      '@pages': path.join(__dirname, 'src/pages'),
    },
  },
};

3.修改package.json文件,用craco替代react-scripts

"scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
  },

4.在 tsconfig.json文件新增

"compilerOptions": {
    // 别名配置
    "baseUrl": "./",
    "paths": {
      "@/*": ["src/*"],
      "@pages/*": ["src/pages/*"]
    },
}

你可能感兴趣的:(react.js,前端,前端框架)