npm i @types/node -D
- tsconfig.json同级新建一个types文件夹
- 新建文件 xxx.d.ts
- 文件内写入
declare module 'xxx'
- tsconfig.json include中加入 types
// tsconfig.json
compilerOptions: {
"baseUrl": "./",
"paths": {
"@/*": [
"src/*"
]
}
}
tsconfig.json -> "jsx": "preserve",
** 在tsx文件中引入样式文件,相当于全局引入,正确的做法:**
react 会将类名做一些修改,类似vue的scoped
- 将样式文件的名字改成 xxx.module.css/less/scss
- tsx文件中引入xxx.module.css/less/scss文件
import styles from './index.scss'
- 将文件以模块化模式导入后,它变成一个对象
export default function Comp1() {
return (
<div className={styles.box}>comp1</div>
)
}
import { Navigate } from 'react-router-dom'
[
{
path: '/',
element: <Navigate to="/home" />
},
{
path: '/',
element: <Layout/>,
children: [
{
path: '/home',
element: <Home/>
}
]
}
]
可能用了懒加载后,需要给组件添加loading,需要用React.Suspense 组件
import React, {lazy} from 'react'
const Home = lazy(() => import('./Home'))
{
path: '/home',
element: <React.Suspense fallback={<div>loading...</div>}>
<Home/>
</React.Suspense>
},
模块样式变量加字符串
<div className={styles.loginBox + ' otherClass'}> => <div class="loginBox otherClass"></div>
const inputChange = (e:ChangeEvent<HTMLInputElement>) => {
// ...
}