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
相关推荐
橘子星15 小时前
在浏览器里跑大模型!用 WebGPU 零成本部署 DeepSeek-R1
前端·typescript
bonechips15 小时前
React + WebGPU:在浏览器里跑一个 DeepSeek 推理模型
react.js·typescript
触底反弹1 天前
🔥 React 零基础入门(上):环境搭建 + JSX 深度解析
前端·react.js·typescript
谷哥的小弟1 天前
TypeScript对象类型
javascript·typescript
大家的林语冰1 天前
🫡 见证历史,TypeScript 7 重写成功,VS Code 原地起飞,GitHub 第一语言联手 Go 破而后立!
前端·javascript·typescript
濮水大叔4 天前
在 DTO 中使用 Form Layout 配置复杂表单,支持 Tabs/Groups/Sections
vue.js·typescript·node.js
用户0934077735145 天前
HarmonyOS WPS Open SDK 实践:用能力分层重构 OpenFileRequest
typescript·harmonyos
gis开发之家5 天前
vue3直击架构痛点 依赖倒置与接口隔离——打造坚如磐石的插件与高阶组件(HOC)模式
javascript·架构·typescript·前端框架·vue3
冬枳5 天前
基于OpenSeagragon封装的前端病理切片展示组件
前端·typescript
退休倒计时6 天前
【每日一题】LeetCode 287. 寻找重复数 TypeScript
算法·leetcode·typescript