The method of requesting applet package promise

// In utils -> base.js in a package Base class, there is a method of Axios 
class Base { 
  constructor () { 

  } 
  Axios (Method, URL, Data) { 
    the console.log ( "call to Axios" ) 
    the let Method Method || = "POST"; // do not pass the POST method by default 
    the let The rootUrl = "https://api.ichongwujia.com/pet-api/" ; 
    the let Data = Data || {}; // do not pass default } is { 
    return  new new Promise ((RES, REJ) => { 
      wx.request ({ 
        Method: Method, 
        URL: The rootUrl + URL, 
        Data: Data, 
        Success (Data) { 
          IF (=== 200 is data.tatusCode) { 
            RES (Data) 
          } the else { 
            RES (Data) 
          } 
        }, 
        Fail (ERR) { 
          REJ (ERR) 
        } 
      }) 
    }) 
  } 
} 
Export default Base; 



// in another js file (index-model.js) , the Base class inheritance, it is a useful method axios 
Import from Base "../../utils/base.js" ; 
class the extends Home Base { 
  constructor () { 
    Super (); 
  } 
} 
Export default Home; 


// in where used on the introduction where 
Import from Home "./index-model.js" ;
Home const = new new Home (); 
Page ({ 
  Data: { 

  }, 
  the onLoad () { 
    // home.axios () // call to print out Axios 
    home.axios ( "the POST", "miniprogram / index", { A:. 1, B: 2 }) 
    .then ((Data) => { 
      the console.log (Data) 
    .}) the catch ((ERR) => { 
      the console.log (ERR) 
    }); 
  } 
})

Guess you like

Origin www.cnblogs.com/wuqilang/p/12070405.html