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
相关推荐
雨雨雨雨雨别下啦18 小时前
【从0开始学前端】TypeScript语法总结
前端·typescript
敲敲了个代码19 小时前
一天面了6个前端开发,水平真的令人堪忧啊
前端·javascript·学习·面试·webpack·typescript·前端框架
DsirNg19 小时前
上一个封装hooks涉及的知识学习路线
前端·javascript·typescript
by__csdn1 天前
Vue 2 与 Vue 3:深度解析与对比
前端·javascript·vue.js·typescript·vue·css3·html5
0***h9421 天前
TypeScript 与后端开发Node.js
javascript·typescript·node.js
Robet1 天前
ts类型工具
typescript
Robet1 天前
类属性公共还是私有
javascript·typescript
x***B4111 天前
TypeScript项目引用
前端·javascript·typescript
心随雨下1 天前
TypeScript泛型开发常见错误解析
java·开发语言·typescript
Robet2 天前
TS和JS成员变量修饰符
javascript·typescript