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、重启收工

相关推荐
阿黎梨梨36 分钟前
React 表单处理与性能优化:从入门到进阶
前端·react.js
Whbbit199936 分钟前
发布自己的 shadcn-vue 扩展组件,并支持 CLI 安装
前端·开源
Tian_Hang1 小时前
Vue3 父子组件通讯
前端·javascript·vue.js
breeze jiang1 小时前
React memo、useCallback 与 useMemo:父组件多状态时如何减少无效渲染
前端·javascript·react.js
朱涛的自习室2 小时前
从 Prompt 到 Graph:AI 工程的进化史
android·前端·人工智能
今天的砖头有点烫手啊3 小时前
AI Agent 开发实战(十):Agent 设计模式(ReAct / Plan-Execute / Reflection)
人工智能·react.js·设计模式
敲代码的玉米C3 小时前
让两个模型一写一审
前端·人工智能·架构
代码不加糖3 小时前
common.js和es6中模块引l入的区别?
前端·javascript·es6
qq83443103 小时前
一款纯前端的excel控件,XuY_Sheet 电子表格组件教程
前端·excel·教程·luckysheet·xuy_sheet控件·前端表格控件
不爱说话郭德纲3 小时前
零基础,学做KMP项目,TRAE Work手把手带你月薪.....
前端·后端·app