如何通过 Angular4 下载网页?

How to download a web page via Angular4?

我想通过 Angular 4.
下载页面 其实我想获取 YouTube 视频的下载链接。
我的代码如下:

import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Jsonp, Http, Response } from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Youtube Direct';
  youtubeLink = 'https://www.youtube.com/watch?v=wBBzD_pDags&list=PL3ewn8T-zRWgWGtHzwrvO2ZdCExU7wk8M';
  youtubePageHtml: string;

  constructor(private jsonp: Jsonp, private http: Http) { }

  GetButtonOnClick(): void {
    this.jsonp.get(this.youtubeLink)
      .map(this.extractData)
      .catch(this.handleError);      
  }

  private extractData(res: Response) {
    console.log(res);
    let body = res.json();
    return body.data || {};
  }

  private handleError(error: Response | any) {
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }


}

但是extractData从不打电话。
如何通过 Angular 4 下载页面?

因为你需要订阅返回的observable。

试试这个:

  GetButtonOnClick(): void {
    this.jsonp.get(this.youtubeLink)
      .map(this.extractData)
      .subscribe(
        (res: any) => this.youtubePageHtml = res,
        (err: Error) => this.handleError,
        () => console.log('OnComplete !')
      )
  }

如果您想了解更多:
- https://angular.io/tutorial/toh-pt6
- https://github.com/ReactiveX/rxjs