Skip to content

仿jira项目笔记 项目起航:第5章 JWT、用户认证与异步请求

仲灏2022-07-18约 1 分钟

service-work 代理

npx imooc-jira-tool

修改src/index.ts

tsx
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { loadDevTools } from "jira-dev-tool";

loadDevTools(() =>
  ReactDOM.render(
    <React.StrictMode>
      <App />
    </React.StrictMode>,
    document.getElementById("root")
  )
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

如果遇到这个错误,需要执行下命令

image-20220719232236141

npx msw init public

注册用户 /login 改为 /register

5-7 用fetch抽象通用HTTP请求方法,增强通用性

5-8 用useHttp管理JWT和登录状态,保持登录状态

tsx
Parameters<typeof http> // 获取http方法入参类型

5-9 TS的联合类型、Partial和Omit介绍

  • 联合类型
let myFavoriteNumber: string | number | {}
  • 交叉类型 &
  • 类型别名

interface 没法实现 Utility type:

如:Parameters<typeof *>

Utility type 就是通过泛型传入其他类型 然后对这个类型进行某种操作

如:对下面类型的所有参数都变成可选类型

tsx
type Person = {
	name: string;
	age: number
}
// 对类型的所有参数都变成可选类型
const XiaoMing: Partial<Person> = {name: 'xiaoming'}

// 只需要大多数属性 去掉小部分属性
const shenMiRen: Omit<Person, 'name'>

Partial 的实现

ts
type Partial<T> = {
  [p in keyof T]?: T[P] 
}

问题

image-20220726234620358

对象为空,因为对象值有很多类型 如对方法结构是不成立的

如果需要明确的键值对 可以这样

export const cleanObject = (object: {[key: string]: unknown}) => {...}

讨论区

欢迎留下想法与补充