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

相关推荐
IT_陈寒2 分钟前
Redis突然吃掉所有内存,我的服务差点挂了
前端·人工智能·后端
2601_958492552 分钟前
Behavioral Analysis of HTML5 Trivia Integration
前端·html·html5
sakiko_9 分钟前
Swift/UIkit学习笔记27-模块管理,发送位置信息
前端·笔记·学习·ios·swift·uikit
hhb_6189 分钟前
Ruby核心技术难点梳理与实战应用案例解析
服务器·前端·ruby
天渺工作室18 分钟前
Vue自定义指令实现点击事件权限拦截控制的npm插件
前端·vue.js·npm
晓得迷路了19 分钟前
栗子前端技术周刊第 129 期 - TanStack npm 供应链入侵事件、pnpm 11.1、Tailwind CSS 4.3...
前端·javascript·css
Lan_Se_Tian_Ma21 分钟前
使用Cursor封装Flutter项目基建框架
前端·人工智能·flutter
ZC跨境爬虫21 分钟前
跟着 MDN 学 HTML day_59:HTML表单与按钮——构建用户交互的基石
前端·javascript·ui·html·交互·媒体
天天开发22 分钟前
Flutter Widget Previewer使用指南:提升开发效率的利器
前端·javascript·flutter
许彰午23 分钟前
IE11富文本兼容——政务系统前端的深渊
前端·政务