記一次使用typescript模板初始化React項目的幾個問題


1、執行 npx create-react-app my-app --typescript 無效

參考 Create React App 官方網站,使用模板初始化React項目的最新指令如下:

npx create-react-app my-app --template [template-name]

附官網地址:https://create-react-app.dev/docs/getting-started/#selecting-a-template

重新運行以下指令安裝

- npm uninstall -g create-react-app
// 或
- yarn global remove create-react-app

npx create-react-app my-app --template typescript

  

2、VsCode 提示 “Cannot use JSX unless the '--jsx' flag is provided”(無法使用 JSX,除非提供了 "--jsx" 標志。)

正確的解決步驟:

(1)、打開Vscode,通過 Ctrl + Shift + P 打開工作區設置

(2)、選擇Typescript 版本

(3)、拋棄Vscode使用的4.0.3版本,選擇較新的4.1.2版本

 

3、啟動項目后,控制台報錯 Uncaught TypeError: Cannot read property 'forEach' of undefined at Object.injectIntoGlobalHook (react-refresh-runtime.development.js:465)

解決辦法:禁用瀏覽器擴展程序中的 React 開發者工具

重新啟動項目,可以正常打開頁面了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM