pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <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="fhhh1nd" 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="phhfhpp" 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="xjz3vdd" 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>
                          主站蜘蛛池模板: 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 首页 - 张店继勇软件开发工作室 兰州UPS电源,兰州山特UPS-兰州万胜商贸 | 干式变压器厂_干式变压器厂家_scb11/scb13/scb10/scb14/scb18干式变压器生产厂家-山东科锐变压器有限公司 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 安徽合肥格力空调专卖店_格力中央空调_格力空调总经销公司代理-皖格制冷设备 | 深圳法律咨询【24小时在线】深圳律师咨询免费 | 河北凯普威医疗器材有限公司,高档轮椅系列,推车系列,座厕椅系列,协步椅系列,拐扙系列,卫浴系列 | 偏心半球阀-电动偏心半球阀-调流调压阀-旋球阀-上欧阀门有限公司 | 安全,主动,被动,柔性,山体滑坡,sns,钢丝绳,边坡,防护网,护栏网,围栏,栏杆,栅栏,厂家 - 护栏网防护网生产厂家 | 轻型地埋电缆故障测试仪,频响法绕组变形测试仪,静荷式卧式拉力试验机-扬州苏电 | 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 | 塑钢课桌椅、学生课桌椅、课桌椅厂家-学仕教育设备首页 | 高楼航空障碍灯厂家哪家好_航空障碍灯厂家_广州北斗星障碍灯有限公司 | 合景一建-无尘车间设计施工_食品医药洁净车间工程装修总承包公司 | 沈阳网站建设_沈阳网站制作_沈阳网页设计-做网站就找示剑新零售 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 | 温控器生产厂家-提供温度开关/热保护器定制与批发-惠州市华恺威电子科技有限公司 | 全自动在线分板机_铣刀式在线分板机_曲线分板机_PCB分板机-东莞市亿协自动化设备有限公司 | 水平垂直燃烧试验仪-灼热丝试验仪-漏电起痕试验仪-针焰试验仪-塑料材料燃烧检测设备-IP防水试验机 | 120kv/2mA直流高压发生器-60kv/2mA-30kva/50kv工频耐压试验装置-旭明电工 | 万博士范文网-您身边的范文参考网站Vanbs.com | 中国在职研究生招生信息网 | EPDM密封胶条-EPDM密封垫片-EPDM生产厂家 | 阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司 | 高压油管,液压接头,液压附件-烟台市正诚液压附件 | 美国PARKER齿轮泵,美国PARKER柱塞泵,美国PARKER叶片泵,美国PARKER电磁阀,美国PARKER比例阀-上海维特锐实业发展有限公司二部 | 三氯异氰尿酸-二氯-三氯-二氯异氰尿酸钠-优氯净-强氯精-消毒片-济南中北_优氯净厂家 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 铝合金线槽_铝型材加工_空调挡水板厂家-江阴炜福金属制品有限公司 | 无硅导热垫片-碳纤维导热垫片-导热相变材料厂家-东莞市盛元新材料科技有限公司 | 磁力反应釜,高压釜,实验室反应釜,高温高压反应釜-威海自控反应釜有限公司 | 牛奶检测仪-乳成分分析仪-北京海谊 | 工装定制/做厂家/公司_工装订做/制价格/费用-北京圣达信工装 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 代办建筑资质升级-建筑资质延期就找上海国信启航 | 高楼航空障碍灯厂家哪家好_航空障碍灯厂家_广州北斗星障碍灯有限公司 | 雷冲击高压发生器-水内冷直流高压发生器-串联谐振分压器-武汉特高压电力科技有限公司 | 重庆私家花园设计-别墅花园-庭院-景观设计-重庆彩木园林建设有限公司 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 智慧钢琴-电钢琴-便携钢琴-数码钢琴-深圳市特伦斯乐器有限公司 |