do in Angular 6

0

I am creating a guard for Angular 6 and I have encountered the problem that operators rxjs no longer go as before. Aside from being imperatively different, I had to put the .map or .task within a .pipe() . This has allowed me to solve the problems. However, I do not see how to replace or import the operator .do .

I show you my code and see if you can help me find an alternative. The error that VSC gives me is:

  

"The module '" c: / Users / upload / Desktop / We rotate CPanel / node_modules / rxjs / operators / index "' does not have any member 'do' exported." and "The property 'do' does not exist in the 'Observable' type.".

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from '../services/auth.service';
import { map, take, do } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {
  constructor (
private router: Router,
private authService: AuthService
  ) {}

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    return this.authService.afAuth.authState.pipe(
    take(1)).pipe(
    map(authState => !! authState)).do(logado => {
      if (!logado) {
        this.router.navigate(['/login']);
      }
    });
  }
}
    
asked by Joan Subirats Llaveria 28.08.2018 в 19:56
source

1 answer

2

As of version 6 of rxjs , there were big changes there because of your problem.

Several operators changed their name, because they matched those of JavaScript , such as:

  • do => tap
  • catch => catchError
  • switch => switchAll
  • finally => finalize

Another important point is that you can not chain operators as you did before, you must use .pipe()

Code Example:

// Antes de la V6
source
 .map(x => x + x)
 .mergeMap(n => of(n + 1, n + 2)
   .filter(x => x % 1 == 0)
   .scan((acc, x) => acc + x, 0)
 )
 .catch(err => of('error found'))
 .subscribe(printResult);

 // Ahora en la V6 se debe hacer de la siguiente forma
 source.pipe(
 map(x => x + x),
 mergeMap(n => of(n + 1, n + 2).pipe(
   filter(x => x % 1 == 0),
   scan((acc, x) => acc + x, 0),
 )),
 catchError(err => of('error found')),
).subscribe(printResult);

Alternative:

  

If you have another old project you could continue using the previous form   installing the following compatibility module, which is not   recommended since it is preferable that you update your code :

     

npm install rxjs-compat@6 --save

Source: link

    
answered by 28.08.2018 / 20:42
source