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>
                          主站蜘蛛池模板: 金属回收_废铜废铁回收_边角料回收_废不锈钢回收_废旧电缆线回收-广东益夫金属回收公司 | 一体化净水器_一体化净水设备_一体化水处理设备-江苏旭浩鑫环保科技有限公司 | 不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰]-不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰] | 潜水搅拌机-双曲面搅拌机-潜水推进器|奥伯尔环保 | 光纤测温-荧光光纤测温系统-福州华光天锐光电科技有限公司 | 槽钢冲孔机,槽钢三面冲,带钢冲孔机-山东兴田阳光智能装备股份有限公司 | 河南不锈钢水箱_地埋水箱_镀锌板水箱_消防水箱厂家-河南联固供水设备有限公司 | 不锈钢轴流风机,不锈钢电机-许昌光维防爆电机有限公司(原许昌光维特种电机技术有限公司) | 常州律师事务所_常州律所_常州律师-江苏乐天律师事务所 | 济南保安公司加盟挂靠-亮剑国际安保服务集团总部-山东保安公司|济南保安培训学校 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | 防堵吹扫装置-防堵风压测量装置-电动操作显示器-兴洲仪器 | 悬浮拼装地板_幼儿园_篮球场_悬浮拼接地板-山东悬浮拼装地板厂家 | 北京遮阳网-防尘盖土网-盖土草坪-迷彩网-防尘网生产厂家-京兴科技 | 爆破器材运输车|烟花爆竹运输车|1-9类危险品厢式运输车|湖北江南专用特种汽车有限公司 | 不锈钢丸厂家,铝丸,铸钢丸-淄博智源铸造材料有限公司 | 酒店品牌设计-酒店vi设计-酒店标识设计【国际级】VI策划公司 | 合肥白癜风医院_合肥治疗白癜风医院_合肥看白癜风医院哪家好_合肥华研白癜风医院 | 焊接减速机箱体,减速机箱体加工-淄博博山泽坤机械厂 | 广东之窗网 | 北京软件开发_软件开发公司_北京软件公司-北京宜天信达软件开发公司 | 茶楼装修设计_茶馆室内设计效果图_云臻轩茶楼装饰公司 | 免费网站网址收录网_海企优网站推荐平台 | 存包柜厂家_电子存包柜_超市存包柜_超市电子存包柜_自动存包柜-洛阳中星 | 颗粒机,颗粒机组,木屑颗粒机-济南劲能机械有限公司 | 南京租车,南京汽车租赁,南京包车,南京会议租车-南京七熹租车 | 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | 宿松新闻网 宿松网|宿松在线|宿松门户|安徽宿松(直管县)|宿松新闻综合网站|宿松官方新闻发布 | 河南空气能热水器-洛阳空气能采暖-洛阳太阳能热水工程-洛阳润达高科空气能商行 | 防爆电机_ybx3系列电机_河南省南洋防爆电机有限公司 | 水厂自动化-水厂控制系统-泵站自动化|控制系统-闸门自动化控制-济南华通中控科技有限公司 | 有声小说,听书,听小说资源库-听世界网 | 凝胶成像系统(wb成像系统)百科-上海嘉鹏 | 照相馆预约系统,微信公众号摄影门店系统,影楼管理软件-盟百网络 | 小小作文网_中小学优秀作文范文大全 | 今日娱乐圈——影视剧集_八卦娱乐_明星八卦_最新娱乐八卦新闻 | 管家婆-管家婆软件-管家婆辉煌-管家婆进销存-管家婆工贸ERP | 微波消解仪器_智能微波消解仪报价_高压微波消解仪厂家_那艾 | 蜘蛛车-登高车-高空作业平台-高空作业车-曲臂剪叉式升降机租赁-重庆海克斯公司 | 工装定制/做厂家/公司_工装订做/制价格/费用-北京圣达信工装 | 电动球阀_不锈钢电动球阀_电动三通球阀_电动调节球阀_上海湖泉阀门有限公司 |