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
相关推荐
mez_Blog1 小时前
个人小结(2.0)
前端·javascript·vue.js·学习·typescript
QGC二次开发4 小时前
Vue3 : Pinia的性质与作用
前端·javascript·vue.js·typescript·前端框架·vue
2301_801074156 小时前
TypeScript异常处理
前端·javascript·typescript
下雪天的夏风9 小时前
TS - tsconfig.json 和 tsconfig.node.json 的关系,如何在TS 中使用 JS 不报错
前端·javascript·typescript
天下无贼!1 天前
2024年最新版TypeScript学习笔记——泛型、接口、枚举、自定义类型等知识点
前端·javascript·vue.js·笔记·学习·typescript·html
Jorah3 天前
1. TypeScript基本语法
javascript·ubuntu·typescript
小白小白从不日白4 天前
TS axios封装
前端·typescript
aimmon4 天前
Superset二次开发之源码DependencyList.tsx 分析
前端·typescript·二次开发·bi·superset
下雪天的夏风5 天前
Vant 按需引入导致 Typescript,eslint 报错问题
前端·typescript·eslint
theMuseCatcher5 天前
Vue3+TypeScript+Vite+Less 开发 H5 项目(amfe-flexible + postcss-pxtorem)
typescript·less·postcss