关于axios:Axios函数柯里化封装-Get

40次阅读

共计 739 个字符,预计需要花费 2 分钟才能阅读完成。

可运行残缺代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="https://unpkg.com/vue@next"></script>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
  <div id="hello-vue">
    {{message}}
  </div>
</body>
<script>
// 函数柯里化
// url 不变,参数变动
function get(url) {return function(params) {return axios.get(url + params)
      .then(res => {console.log('res', res.data)
      })
  }
}
// 
const HelloVueApp = {data() {
    return {message: 'Hello Vue!!'}
  },
  mounted() {
    const baseUrl = 'https://www.fastmock.site/mock/320dcea3c1fbc2abb8bc0f60c25569f7/api'
    // axios.get(`${baseUrl}/blog/1`)
    //   .then((res) => {//     console.log('res', res.data)
    //   })
    const getBlog = get(`${baseUrl}/blog/`)
    getBlog(1)
    getBlog(2)
  },
}
// 
Vue.createApp(HelloVueApp).mount('#hello-vue')
</script>
</html>

正文完
 0