码迷,mamicode.com
首页 > 其他好文 > 详细

31封装一个网络请求的服务

时间:2020-01-23 09:48:33      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:ide   nbsp   str   provider   范围   ring   作用   any   请求   

①创建一个服务类

  @Injectable()

  在服务类中定义方法、数据

  sendRequest ( myUrl : string ) {

    return this.http.get( myUrl ).map(  (response : Response) => response.json()  )

  }

②给服务指定提供商

  providers : [ MyHttpService ]  模块、组件限定作用范围

③调用服务

  import { }

  this.myHttpService.sendRequset( ‘ 请求的地址 ‘ ).subscribe ( (result : any) => {  console.log (reuslt)})

31封装一个网络请求的服务

标签:ide   nbsp   str   provider   范围   ring   作用   any   请求   

原文地址:https://www.cnblogs.com/shanlu0000/p/12230051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!