typescript manual

这里写目录标题

throw new Error

throw new Error("Get data error") 是在浏览器的Console中显示错误信息。

在浏览器中调试Json

定义类型

定义数组

// 用于初始化空列表

userList: [] as UserInfo[],

// 用于尚未加载的数据

user: null as UserInfo | null,

function

Named function

css 复制代码
function add(x: number, y: number): number {
  return x + y;
}

anonymous function

css 复制代码
let myAdd = function (x: number, y: number): number {
  return x + y;
};

Axios

经典片段

css 复制代码
 const ax = axios.create({
  baseURL: 'yourbaseUrl',
  withCredentials: true,
});

const loginUser = () => { const body ={username:state.values.email, password:state.values.password};
ax.post('/login',body).then(function(response){
return response}).then().catch(error => console.log(error));}

错误及解决

ref value

ref是一个对象,必须通过value才能使用

css 复制代码
  const onSubmit = async () => {
    await saveOrUpdate(form)
  }
 修正后
const onSubmit = async () => {
    await saveOrUpdate(form.value)
  }

because it is a constant

css 复制代码
Cannot assign to 'menuArr' because it is a constant.
css 复制代码
const menuArr = ref([] as MenuItem[])
menuArr = data.content

因为 ref 定义的时一个 constant 常量,赋值的时候是对常量的值进行赋值,而不是常量。

css 复制代码
menuArr.value = data.content

API 和 客户端定义的数据结构不一样

错误信息,并不是服务器端没有返回数据,而是双方的数据结果不同导致不能赋值。

css 复制代码
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data')

Server

css 复制代码
export type UserResponse = {
  success: string
  code: number
  message: string
  content: {
    id?: number
    userName?: string
    userPasswd?: string
    userFirstName?: string
  }
}

Client

css 复制代码
export interface UserProfile {
  id?: number
  userName?: string
  userPasswd?: string
相关推荐
tech_zjf2 小时前
装饰器:给你的代码穿上品如的衣服
前端·typescript·代码规范
三棵杨树4 小时前
TypeScript从零开始(三):基础类型下
typescript
zoomdong6 小时前
10x 提升!TypeScript 宣布使用 Go 重写
前端·typescript
蒜香拿铁9 小时前
【typescript基础篇】(第三章) 函数
前端·typescript
蒜香拿铁9 小时前
【typescript基础篇】(第三章) 接口
前端·typescript
kangyouwei9 小时前
TS中Omit如何在enum枚举类型上使用
typescript
只会写Bug的程序员10 小时前
面试之《TypeScript泛型》
前端·面试·typescript
青春路上的小蜜蜂21 小时前
鸿蒙——实操开发自定义Hivigor插件并发布插件
typescript·harmonyos·plugin·hvigor·自定义插件
觉醒法师1 天前
HarmonyOS开发 - 电商App实例二( 网络请求http)
前端·http·华为·typescript·harmonyos·ark-ts
shmily_yy1 天前
Ts支持哪些类型和类型运算(下)
前端·typescript