使用fetch请求数据

第一种写法:

  getData() {
    fetch('/api/list_all').then(res => {
      return res.json()
    })
    .then(res => {
      if(res.code === 200) {

      }
    })
    .catch(err => {
      console.log(err)
    })
  }

第二种写法:

  async getData() {
    try {
      const result = await fetch('/api/list_all')
      const res = await result.json()
      if (res.code === 200) {

      }
    } catch (error) {
      console.log(error)
    }
  }
原文地址:https://www.cnblogs.com/xutongbao/p/14876291.html