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
相关推荐
用户938515635074 小时前
从零在浏览器里跑 DeepSeek-R1:WebGPU + Transformer.js 全链路实战
前端·设计模式·typescript
子兮曰9 小时前
Bun vs Node.js 深度对决:跑分快 4 倍,真实业务只剩 3%,2026 年到底该怎么选?
前端·后端·typescript
水獭比特1 天前
Agent 的 timeout 为什么会失效?把一条总预算传到底
人工智能·typescript
xcLeigh1 天前
编程语言的 AI 友好度排名:Python、JavaScript、TypeScript 谁更适合 AI 辅助
javascript·人工智能·python·ai·typescript·ai编程
谷哥的小弟1 天前
TypeScript中的any
javascript·typescript
谷哥的小弟1 天前
TypeScript在Vue3中的常见写法
前端·javascript·typescript
njsgcs1 天前
链轮 平键和键槽的标准尺寸 计算 typescript
typescript·机械工程
满栀5852 天前
请求拦截、响应拦截、业务错误统一处理
前端·typescript·anti-design-vue
谷哥的小弟2 天前
TypeScript接口
javascript·typescript
谷哥的小弟2 天前
TypeScript类型断言
前端·javascript·typescript