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

①创建一个服务类

  @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)})

 

demo27/

posted @ 2020-01-23 01:43  shanlu  阅读(123)  评论(0编辑  收藏  举报