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

猜你喜欢

转载自www.cnblogs.com/shanlu0000/p/12230051.html
今日推荐