为什么 Angular 教程不需要导入 Observable
Why Angular Tutorials dont need to import Observable
如果您查看 angular 教程 here,他们不会像其他人一样导入 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 行。
Here is what I did bare minimum to get ts to not complain
这是正确的版本。 angular 文档完全不准确/已过时。向他们发送 PR
Its just not shown there , For code readability, but if you go through
their tutorial :
有link现场演示: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';
如果您查看 angular 教程 here,他们不会像其他人一样导入 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 行。
Here is what I did bare minimum to get ts to not complain
这是正确的版本。 angular 文档完全不准确/已过时。向他们发送 PR
Its just not shown there , For code readability, but if you go through their tutorial :
有link现场演示: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';