react typescript @别名的使用

1、config/webpack.config.js中找到alias,添加"@": path.resolve(__dirname,'../src'),如下:

TypeScript 复制代码
alias: {
        // Support React Native Web
        // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
        "react-native": "react-native-web",
        // Allows for better profiling with ReactDevTools
        ...(isEnvProductionProfile && {
          "react-dom$": "react-dom/profiling",
          "scheduler/tracing": "scheduler/tracing-profiling",
        }),
        ...(modules.webpackAliases || {}),
        "@": path.resolve(__dirname,'../src')
},

2、tsconfg.json中配置,新增"paths":{"@/*":"./src/\*"}

TypeScript 复制代码
{
    "compilerOptions": {
        "target": "es5",
        "lib": [
            "dom",
            "dom.iterable",
            "esnext"
        ],
        "allowJs": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "noFallthroughCasesInSwitch": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react-jsx",
        "paths": {
            "@/*": ["./src/*"]
        }
    },
    "include": [
        "src"
    ]
}

3、重启收工

相关推荐
Rkgua2 分钟前
TS中`Function`、`CallableFunction` 和 `NewableFunction`的函数区别
前端
Asize4 分钟前
重生之我在 Vibe Coding 时代当程序员:第十一课,JS底层 :变量提升真相
前端·javascript
HYCS4 分钟前
用pixi.js实现fabric.js(五):事件系统
前端·javascript·canvas
Momo__7 分钟前
Node.js 26 来了:Temporal API 默认启用,Date 终于可以退休了
前端·node.js
雨季mo浅忆11 分钟前
记录前端内网开发之新入职篇
前端·内网开发
杨运交24 分钟前
[025][Web模块]基于 Spring Boot 的请求日志过滤器设计与实现
前端·spring boot·后端
孟陬27 分钟前
首次上榜新项目 HyperFrames(22k Star):HTML → MP4 一句话生成视频
react.js·node.js·html
IT_陈寒33 分钟前
React的useEffect里设状态?我又踩雷了
前端·人工智能·后端
恋猫de小郭36 分钟前
GSY 史上最全跨平台/架构/语言的项目,七大项目召唤「神龙」
android·前端·flutter
wgc2k40 分钟前
Nest.js基础-5:关于Docker的简单概述
docker·typescript·node.js