Skip to content

向接口发送请求

ts
async function getData() {// async声明函数式异步完成的
    let result = await axios.get('https://dog.ceo/api/breed/pembroke/images/random')
    console.log(result.data)
    pic.push(result.data.message)
}

await 会暂停这个异步函数的执行直到 get 返回结果,由于 JS 的异步模型,这不会导致线程阻塞。

连续对象解构+重命名

ts
interface list{
    id:string,
    title:string
}

let talkList = reactive<list[]>([])//类型限定,以免出现never类型

async function getAword() {
    const {data:{content:title}} = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
    let obj:list = {
        id:nanoid(),
        title:title
    }
    console.log(obj)
    talkList.push(obj)
}

hook