2

如果你看这里的角度教程,他们不会像其他人一样导入 observable,他们也不会导入他们的 authService。

这是我为让 ts 不抱怨所做的最低限度的工作:

import { AuthService } from './auth.service';
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  constructor(private auth: AuthService) {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // Get the auth header from the service.
    const authHeader = this.auth.getAuthorizationHeader();
    // Clone the request to add the new header.
    const authReq = req.clone({headers: req.headers.set('Authorization', authHeader)});
    // Pass on the cloned request instead of the original request.
    return next.handle(authReq);
  }
}

他们是在做我没有做的事情,还是他们只是跳过导入以使教程缩短 2 行。

4

2 回答 2

2

它只是没有显示在那里,为了代码的可读性,但如果你通过他们的教程:

https://angular.io/tutorial/toh-pt6#rxjs-imports

有现场演示的链接:Plnkr

在这里你可以清楚地看到所有的导入

文件:app/hero-search.component.ts

import { Component, OnInit } from '@angular/core';
import { Router }            from '@angular/router';

import { Observable }        from 'rxjs/Observable';
import { Subject }           from 'rxjs/Subject';

// Observable class extensions
import 'rxjs/add/observable/of';

// Observable operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
于 2017-11-03T04:20:24.643 回答
1

这是我为让 ts 不抱怨所做的最低限度的工作

这是正确的版本。角度文档根本不准确/过时。向他们发送 PR

于 2017-11-03T02:18:04.150 回答