2

If you look at the angular tutorials here, they do not import observable like everyone else has to, also they don't import their authService.

Here is what I did bare minimum to get ts to not complain:

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);
  }
}

Are they doing something that I am not, or did they just skip importing to keep the tutorial shorter by 2 lines.

2
  • 2
    I looks like they've omitted any non-Angular imports for the sake of brevity. There is no magic that I'm aware of that would do away with the need for the imports you've mentioned. Commented Nov 3, 2017 at 0:31
  • ok thanks. That is what i was assuming, but I was hoping there was some magic I could do. Commented Nov 3, 2017 at 0:36

2 Answers 2

2

Its just not shown there , For code readability, but if you go through their tutorial :

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

There is link to the live demo : Plnkr

Here you can clearly see all the imports

File : 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';
Sign up to request clarification or add additional context in comments.

Comments

1

Here is what I did bare minimum to get ts to not complain

This is right version. The angular docs are simply inaccurate / out of date. Send them a PR 🌹

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.