久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

    1. <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 class="qwawimqqmiuu" id='ahsmW'><tfoot id='ahsmW'></tfoot><dl id='ahsmW'><fieldset id='ahsmW'></fieldset></dl></div>

      Angular 2 - 從承諾中返回 HTTP

      Angular 2 - Return HTTP from inside a promise(Angular 2 - 從承諾中返回 HTTP)

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

          <tbody id='Yn94i'></tbody>

        • <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 class="qwawimqqmiuu" 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的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                在我的 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模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Use IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
                anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 項目中不起作用)
                Ionic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用異步數據更新 Observable)
                Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
                In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何創建使用 Ionic 組件的自定義指令?)
                Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(將 ViewChild 用于動態元素 - Angular 2 amp;離子2)
                    <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 class="qwawimqqmiuu" id='kYhX2'><tfoot id='kYhX2'></tfoot><dl id='kYhX2'><fieldset id='kYhX2'></fieldset></dl></div>

                    3. <tfoot id='kYhX2'></tfoot>
                        <tbody id='kYhX2'></tbody>
                        <bdo id='kYhX2'></bdo><ul id='kYhX2'></ul>
                          主站蜘蛛池模板: 国产精品福利在线观看 | 99亚洲 | 欧美国产视频 | 91精品久久久久久久久久入口 | 欧美久久视频 | 成人做爰www免费看视频网站 | 亚洲精品免费看 | 午夜免费网站 | 国产在线精品区 | 国产精品久久久久久av公交车 | 天堂网色 | 成人免费一区二区三区牛牛 | 伊人免费在线观看 | 另类 综合 日韩 欧美 亚洲 | 国产这里只有精品 | 在线播放一区二区三区 | 国产99久久精品一区二区永久免费 | 91久久国产综合久久 | 日韩欧美三级 | aaaaaa大片免费看最大的 | 午夜网站视频 | 精品久久99 | 一区二区三区成人 | 黄色大片毛片 | 国产精品久久久久久久久久软件 | 丁香婷婷久久久综合精品国产 | av香港经典三级级 在线 | 亚洲综合一区二区三区 | 亚洲一区二区免费 | 国产一区二区在线视频 | 久久久精品一区二区三区 | 在线播放中文字幕 | 亚洲二区精品 | 日韩一区二区三区视频在线观看 | 免费在线观看av的网站 | 日韩一区二区在线播放 | 成人a视频片观看免费 | 91麻豆精品一区二区三区 | 在线观看黄免费 | 中国大陆高清aⅴ毛片 | 高清成人免费视频 |