采取(1)与第一个() [英] take(1) vs first()

查看:24
本文介绍了采取(1)与第一个()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些使用 take(1)AuthGuard 实现.在我的项目中,我使用了 first().

两者的工作方式相同吗?

import 'rxjs/add/operator/map';导入 'rxjs/add/operator/first';从 'rxjs/Observable' 导入 { Observable };从@angular/core"导入{可注射};从@angular/router"导入 { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot };从 'angularfire2' 导入 { AngularFire };@Injectable()导出类 AuthGuard 实现 CanActivate {构造函数(私有angularFire:AngularFire,私有路由器:路由器){}canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable|布尔{返回 this.angularFire.auth.map((认证) =>{如果(认证){this.router.navigate(['/dashboard']);返回假;} 别的 {返回真;}}).第一的();//只需将其更改为 .take(1)}}

解决方案

运算符 first()take(1) 不一样.

first() 运算符采用可选的 predicate 函数,并在源完成后没有匹配的值时发出 error 通知.>

例如,这将发出错误:

import { EMPTY, range } from 'rxjs';import { first, take } from 'rxjs/operators';空管道(第一的(),).subscribe(console.log, err => console.log('Error', err));

...还有这个:

range(1, 5).pipe(首先(val => val > 6),).subscribe(console.log, err => console.log('Error', err));

虽然这将匹配发出的第一个值:

range(1, 5).pipe(第一的(),).subscribe(console.log, err => console.log('Error', err));

另一方面 take(1) 只是取第一个值并完成.不涉及进一步的逻辑.

range(1, 5).pipe(采取(1),).subscribe(console.log, err => console.log('Error', err));

然后使用空源 Observable 它不会发出任何错误:

EMPTY.pipe(采取(1),).subscribe(console.log, err => console.log('Error', err));

2019 年 1 月:针对 RxJS 6 更新

I found a few implementation of AuthGuards that use take(1). In my project, I used first().

Do both work the same way?

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/first';
import { Observable } from 'rxjs/Observable';

import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AngularFire } from 'angularfire2';

@Injectable()
export class AuthGuard implements CanActivate {

    constructor(private angularFire: AngularFire, private router: Router) { }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
        return this.angularFire.auth.map(
            (auth) =>  {
                if (auth) {
                    this.router.navigate(['/dashboard']);
                    return false;
                } else {
                    return true;
                }
            }
        ).first(); // Just change this to .take(1)
    }
}

解决方案

Operators first() and take(1) aren't the same.

The first() operator takes an optional predicate function and emits an error notification when no value matched when the source completed.

For example this will emit an error:

import { EMPTY, range } from 'rxjs';
import { first, take } from 'rxjs/operators';

EMPTY.pipe(
  first(),
).subscribe(console.log, err => console.log('Error', err));

... as well as this:

range(1, 5).pipe(
  first(val => val > 6),
).subscribe(console.log, err => console.log('Error', err));

While this will match the first value emitted:

range(1, 5).pipe(
  first(),
).subscribe(console.log, err => console.log('Error', err));

On the other hand take(1) just takes the first value and completes. No further logic is involved.

range(1, 5).pipe(
  take(1),
).subscribe(console.log, err => console.log('Error', err));

Then with empty source Observable it won't emit any error:

EMPTY.pipe(
  take(1),
).subscribe(console.log, err => console.log('Error', err));

Jan 2019: Updated for RxJS 6

这篇关于采取(1)与第一个()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆