【问题标题】:Angular 2 - Inject custom headers on iframeAngular 2 - 在 iframe 上注入自定义标头
【发布时间】:2017-05-03 14:34:57
【问题描述】:

尝试将请求自定义标头(类似于'AUTH-TOKEN':'SomeToken123')注入到 Angular 4 上,我快疯了。

我需要将一些必需的自定义标头参数传递给 iframe 页面。

谁能帮帮我?

foo.component.html

<iframe [hidden]="isLoading" class="full" #iframe [src]="secureSrc" (load)="onIframeLoad()" frameborder="0" ></iframe>

组件.ts

@Component({
    selector: 'app-foo',
    templateUrl: './foo.component.html'
})

export class FooComponent implements OnInit {

    @ViewChild('iframe') iframe: ElementRef;
    public isLoading: Boolean;
    public secureSrc: SafeResourceUrl;

    constructor(
        private sanitizer: DomSanitizer,
        private renderer: Renderer2,
        private router: Router
    ) {  }

    public ngOnInit() {
        this.isLoading = true;

        this.secureSrc = this.getIframeURL();
    }

    private getIframeURL(): SafeResourceUrl {
        return this.sanitizer
            .bypassSecurityTrustResourceUrl('https://iframe.address');
    }

    public onIframeLoad(): void {
        if (typeof this.iframe !== 'undefined') {

            // Once iFrame Loaded
            if (typeof this.token !== 'undefined') {
                this.iframe
                    .nativeElement
                    .contentWindow
                    .postMessage({
                        externalCommand: 'some-action',
                        parameter : 'action parameter'
                    }, '*');
            }

            this.isLoading = false;
        }
    }
}

谢谢!

【问题讨论】:

    标签: javascript angular typescript iframe typescript2.0


    【解决方案1】:

    你可以这样做:

    1. 创建一个服务,该服务将连同标头一起发送 http get 请求, 并期待 blob 响应。
    2. 在您的组件中使用该服务来设置 iframe 的 src。
    3. 从 iframe 中删除 [src]="secureSrc" 并仅保留 #iframe

    .

    // service
    import { Injectable } from '@angular/core';
    import { ResponseContentType, Http, RequestOptions, Headers } from '@angular/http';
    import { Observable } from 'rxjs/Observable';
    import { Subscriber } from 'rxjs/Subscriber';
    
    @Injectable()
    export class UrlHelperService {
        constructor(private http: Http) {
        }
    
        get(url: string): Observable<any> {
            let options = new RequestOptions();
            options.headers = new Headers();
            options.headers.append('AUTH-TOKEN', 'SomeToken123');
            options.responseType = ResponseContentType.Blob;
    
            return new Observable((observer: Subscriber<any>) => {
                let objectUrl: string = null;
    
                this.http
                    .get(url, options)
                    .subscribe(m => {
                        objectUrl = URL.createObjectURL(m.blob());
                        observer.next(objectUrl);
                    });
    
                return () => {
                    if (objectUrl) {
                        URL.revokeObjectURL(objectUrl);
                        objectUrl = null;
                    }
                };
            });
        }
    }
    
    // component
    import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
    import { UrlHelperService } from './url-helper.service';  
    
    @Component({
      selector: '',
      templateUrl: './my-app.component.html'
    })   
    export class MyAppComponent implements OnInit {
      @ViewChild('iframe') iframe: ElementRef;
    
      constructor(private urlHelperService: UrlHelperService) { }
    
      ngOnInit() {
        this.urlHelperService.get('http://localhost/api/file/123')
          .subscribe(blob => this.iframe.nativeElement.src = blob);
      }
    }
    

    【讨论】:

    • 我在 Angular 8 中尝试这个。当我尝试时,我无法将 getIframeURL() 的返回传递给 get(),因为 getIframeURL() 返回 SafeResourceUrl 类型,但 get() 需要字符串。此外,如果没有 bypassSecurityTrustResourceUrl,Javascript 将无法正常工作。我该如何克服这种情况?
    • 这个答案是针对 Angular 4 的。Angular 不断发展,所以对于以后的版本需要寻找新的解决方案
    猜你喜欢
    • 2018-12-31
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 2011-12-27
    • 2017-07-28
    • 2017-08-14
    • 1970-01-01
    相关资源
    最近更新 更多