默認(rèn)導(dǎo)入報(bào)錯(cuò)
錯(cuò)誤信息:can only be default-imported using the 'esModuleInterop' flag"
錯(cuò)誤原因:需要在 tsconfig.json
文件中添加 "esModuleInterop": true,
;
參考文檔:https://www.typescriptlang.org/v2/docs/handbook/release-notes/typescript-2-7.html
已經(jīng)安裝了 react-router-dom操灿,但是還是顯示找不到 react-router-dom 模塊
錯(cuò)誤信息: TS2307: Cannot find module 'react-router-dom'.
錯(cuò)誤原因:需要同時(shí)安裝 @types/react-router-dom
// 確保兩個(gè)都有安裝
> yarn add react-router-dom -d
> yarn add @type/react-router-dom -D
tsx 文件不能直接 import Comp from './Comp'
導(dǎo)入,必須寫全路徑導(dǎo)入(即:import Comp from './Comp.tsx'
)
錯(cuò)誤信息:ERROR in ./src/index.tsx
Module not found: Error: Can't resolve './components/BlogList' in '/Users/xuqiang/easyops/node-blog/koa2-front/src'
@ ./src/index.tsx 4:0-49 7:91-99
錯(cuò)誤原因: 當(dāng)我們之前以 import Comp from './Comp'
導(dǎo)入模塊的時(shí)候,實(shí)際上我們是不能確定導(dǎo)入的文件類型是什么茵宪,可能是 ts
,可能是 tsx
环础,也可能是 json
毙驯。所以我們需要在 webpack
里面配置 resolve.extensions
來(lái)幫助我們解析。具體配置如下圖:
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx']
},
參數(shù)文檔:resolve.extensions