//过一秒以后撤销刚刚的异步请求let d =this.requestService.getRxjsData().subscribe(res=>{//在撤销时如果未获取到数据,则不会打印
console.log("rxjs---撤销",res);});setTimeout(()=>{
d.unsubscribe();//取消订阅,取消方法的执行},1000);
getData(){
console.log("执行get请求");let api ="http://a.itying.com/api/productlist"this.http.get(api).subscribe((res:any)=>{this.list=res.result;});}
HttpClient直接post请求
postData(){//手动设置请求的类型 固定写法const httpOptions ={headers:newHttpHeaders({'Content-type':'application/json'})}let api ='https://www.baidu.com/?tn=62095104_43_oem_dg'this.http.post(api,{userName:''},httpOptions).subscribe(res=>{//获取返回的数据})
console.log("执行post请求");}
HttpClient/jsonp进行数据请求
getJsonp(){let api ="http://a.itying.com/api/productlist"this.http.jsonp(api,'callback').subscribe((res:any)=>{this.list=res.result;})}
使用promise/axios进行数据请求
//使用第三方模块实现数据请求axiosGet(api:any){returnnewPromise((resolve, reject)=>{
axios.get(api).then(res=>{
console.log(res);resolve(res.data);})})getAxios(){let api ="http://a.itying.com/api/productlist"this.axiosGet(api).then((res:any)=>{this.list=res.result;})}