1. 安装

  1. // 使用 npm
  2. $ npm install axios
  3. // 使用 bower
  4. $ bower install axios
  5. // 使用 cdn
  6. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

2. axios的实现

  1. // axios是一个http的请求库
  2. var url ="http://192.168.4.18:3000/search?keywords=你";
  3. // axios.get(url).then(res=>{
  4. // console.log(res)
  5. // })
  6. axios.get("http://192.168.4.18:3000/search",{
  7. params:{
  8. keywords:"你"
  9. }//问号后面的值
  10. }).then(res=>{
  11. console.log(res)
  12. })

或(封装url):

  1. var url ="/search"
  2. axios({
  3. url,
  4. baseURL:"http://192.168.4.18:3000",
  5. method:"get",
  6. params:{
  7. keywords:"你"
  8. }
  9. }).then(res=>{
  10. console.log(res)
  11. })

捕获axios.PNG

封装的axios

封装的axios.html

  1. http({
  2. url:"/album?id=96964667"
  3. }).then(res=>{
  4. console.log(res)
  5. })
  6. http({
  7. url:"/album",
  8. params:{
  9. id:"96964667"
  10. }
  11. }).then(res=>{
  12. console.log(res)
  13. })

http.js

  1. var baseURL ="http://192.168.4.18:3000/"
  2. function http({
  3. url,
  4. method="get",
  5. params={}
  6. }){
  7. return axios({
  8. url,
  9. baseURL,
  10. method,
  11. params
  12. })
  13. }