如果你看一下 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 行。
最佳答案
Its just not shown there , For code readability, but if you go through their tutorial :
有现场演示的链接: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 - 为什么 Angular 教程不需要导入 Observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47086667/