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

相关推荐
金木讲编程11 小时前
Claude、Agent与Copilot协作生成Angular应用
前端·ai编程
振华OPPO11 小时前
Vue:“onMounted“ is defined but never used no-unused-vars
前端·javascript·css·vue.js·前端框架
欧雷殿12 小时前
在富阳银湖成立地域化的软件研发团队
前端·程序员·创业
狂炫冰美式12 小时前
前端实时推送 & WebSocket 面试题(2026版)
前端·http·面试
JefferyXZF13 小时前
新手建站零门槛!Vercel+Cloudflare+Namesilo域名购买部署全流程
前端
yinuo13 小时前
微信浏览器缓存机制大揭秘:为什么你总刷不出新页面?
前端
拉不动的猪13 小时前
try...catch 核心与生态协作全解析
前端·javascript·vue.js
Xeon_CC13 小时前
在react-app-rewired工程项目中,调试AntVG6库源码包。
前端·react.js·前端框架
o***Z44814 小时前
前端无障碍开发检查清单,WCAG合规
前端
码上成长14 小时前
React 18 并发特性:useTransition 和 useDeferredValue 动画级解释
javascript·react.js·ecmascript