<tfoot id='Yn94i'></tfoot>

  • <i id='Yn94i'><tr id='Yn94i'><dt id='Yn94i'><q id='Yn94i'><span id='Yn94i'><b id='Yn94i'><form id='Yn94i'><ins id='Yn94i'></ins><ul id='Yn94i'></ul><sub id='Yn94i'></sub></form><legend id='Yn94i'></legend><bdo id='Yn94i'><pre id='Yn94i'><center id='Yn94i'></center></pre></bdo></b><th id='Yn94i'></th></span></q></dt></tr></i><div id='Yn94i'><tfoot id='Yn94i'></tfoot><dl id='Yn94i'><fieldset id='Yn94i'></fieldset></dl></div>
      <bdo id='Yn94i'></bdo><ul id='Yn94i'></ul>

    <legend id='Yn94i'><style id='Yn94i'><dir id='Yn94i'><q id='Yn94i'></q></dir></style></legend>
      1. <small id='Yn94i'></small><noframes id='Yn94i'>

        Angular 2 - 从承诺中返回 HTTP

        时间:2023-09-08
            <bdo id='ahsmW'></bdo><ul id='ahsmW'></ul>
            <tfoot id='ahsmW'></tfoot>

                  <small id='ahsmW'></small><noframes id='ahsmW'>

                  <legend id='ahsmW'><style id='ahsmW'><dir id='ahsmW'><q id='ahsmW'></q></dir></style></legend>
                    <tbody id='ahsmW'></tbody>

                • <i id='ahsmW'><tr id='ahsmW'><dt id='ahsmW'><q id='ahsmW'><span id='ahsmW'><b id='ahsmW'><form id='ahsmW'><ins id='ahsmW'></ins><ul id='ahsmW'></ul><sub id='ahsmW'></sub></form><legend id='ahsmW'></legend><bdo id='ahsmW'><pre id='ahsmW'><center id='ahsmW'></center></pre></bdo></b><th id='ahsmW'></th></span></q></dt></tr></i><div id='ahsmW'><tfoot id='ahsmW'></tfoot><dl id='ahsmW'><fieldset id='ahsmW'></fieldset></dl></div>
                  本文介绍了Angular 2 - 从承诺中返回 HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在我的 api 服务中的每个 http 调用之前,我想检查我的本地存储是否有访问令牌,然后在我拥有它后进行调用.看起来是这样的

                  Before each http call in my api service I want to check my local storage for an access token, then make the call once I have it. It looks like this

                  read(endpoint,params?) {
                  
                      var url: string = this.authService.apiUrl + endpoint, 
                          headers: Headers = new Headers(),
                          queryString: URLSearchParams = new URLSearchParams();
                  
                      this.sessionService.getToken()
                        .then((value) => {
                  
                          queryString.set('access_token', value);
                  
                          headers.append('Content-Type', 'application/json; charset=utf-8');
                  
                          return this.http.get(url,{
                            headers: headers, 
                            search: queryString
                          })
                          .map(res => res.json())
                  
                        });
                  
                  
                    }
                  

                  在我的组件中,我会有类似的东西

                  And in my component I would have something like

                    getData() {
                      this.apiService.read('some endpoint')
                        .subscribe(
                          res => console.log(res),
                          error => this.logError(error)
                        )
                    }
                  

                  这一直有效,直到我在检查本地存储后将 http 调用放入 .then 中.所以我认为它现在嵌套不正确.

                  This was working until I put the http call inside of the .then after checking the local storage. So I think it is now nested incorrectly.

                  解决这个问题的正确方法是什么?在此设置中,是否有更有效的方式从本地存储中获取我的令牌?注意:我使用的是 Ionic 2,它有自己的功能来检查返回承诺的本地存储.

                  What is the correct way to appraoch this? And is there perhaps a more efficent way to grab my token from local storage in this set up? NOTE: I am using Ionic 2 which has its own function for checking local storage which returns a promise.

                  任何建议都会很棒.

                  谢谢.

                  推荐答案

                  您必须将 http observable 转换为 promise 或将 promise 转换为 observable.

                  You will have to convert the http observable to a promise or convert promise to an observable.

                  可观察到的承诺:

                  read(endpoint,params?) {
                  
                      var url: string = this.authService.apiUrl + endpoint, 
                          headers: Headers = new Headers(),
                          queryString: URLSearchParams = new URLSearchParams();
                  
                      return this.sessionService.getToken() //return the outer promise
                        .then((value) => {
                  
                          queryString.set('access_token', value);
                  
                          headers.append('Content-Type', 'application/json; charset=utf-8');
                  
                          return this.http.get(url,{
                            headers: headers, 
                            search: queryString
                          })
                          .map(res => res.json()).toPromise() //use Observable.toPromise
                  
                        });
                  
                  
                    }
                  

                  调用使用

                  this.apiService.read('some endpoint').then((data)=>{}).catch(err=>{})
                  

                  对 Observable 的承诺:

                  Promise to Observable:

                  read(endpoint,params?) {
                  
                      var url: string = this.authService.apiUrl + endpoint, 
                          headers: Headers = new Headers(),
                          queryString: URLSearchParams = new URLSearchParams();
                  
                      return Observable.fromPromise(this.sessionService.getToken())//convert to Promise and return chain.
                        .switchMap((value) => {//use Observable.switchMap to move to second observable
                  
                          queryString.set('access_token', value);
                  
                          headers.append('Content-Type', 'application/json; charset=utf-8');
                  
                          return this.http.get(url,{
                            headers: headers, 
                            search: queryString
                          })
                          .map(res => res.json())
                  
                        });
                  
                  
                    }
                  

                  RXJS 5.5 开始:

                  对 Observable 的承诺:

                  Promise to Observable:

                  read(endpoint,params?) {
                  
                      var url: string = this.authService.apiUrl + endpoint, 
                          headers: Headers = new Headers(),
                          queryString: URLSearchParams = new URLSearchParams();
                  
                      return fromPromise(this.sessionService.getToken())//import {fromPromise } from "rxjs/observables/fromPromise";
                       .pipe(
                        switchMap((value) => {//import {switchMap} from 'rxjs/operators/switchMap';
                  
                          queryString.set('access_token', value);
                  
                          headers.append('Content-Type', 'application/json; charset=utf-8');
                  
                          return this.http.get(url,{
                            headers: headers, 
                            search: queryString
                          })
                        });
                       );
                  
                    }
                  

                  这篇关于Angular 2 - 从承诺中返回 HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:关闭应用程序时收听 Firebase 数据库更改 下一篇:如何将点击事件添加到打字稿中动态添加的html元

                  相关文章

                  最新文章

                    <legend id='kYhX2'><style id='kYhX2'><dir id='kYhX2'><q id='kYhX2'></q></dir></style></legend>

                    1. <small id='kYhX2'></small><noframes id='kYhX2'>

                    2. <i id='kYhX2'><tr id='kYhX2'><dt id='kYhX2'><q id='kYhX2'><span id='kYhX2'><b id='kYhX2'><form id='kYhX2'><ins id='kYhX2'></ins><ul id='kYhX2'></ul><sub id='kYhX2'></sub></form><legend id='kYhX2'></legend><bdo id='kYhX2'><pre id='kYhX2'><center id='kYhX2'></center></pre></bdo></b><th id='kYhX2'></th></span></q></dt></tr></i><div id='kYhX2'><tfoot id='kYhX2'></tfoot><dl id='kYhX2'><fieldset id='kYhX2'></fieldset></dl></div>

                    3. <tfoot id='kYhX2'></tfoot>
                      • <bdo id='kYhX2'></bdo><ul id='kYhX2'></ul>