类型'Observable <event>'上不存在属性'distinctUntilChanged'

时间:2018-10-11 11:28:21

标签: angular angular5 angular6 rxjs5 rxjs6

尝试从5升级到Angular 6并收到此错误:

  src / app / app.component.ts(26,23)中的

ERROR:错误TS2339:属性   类型“可观察”不存在“ distinctUntilChanged”。

我已经在app.module.ts中导入了distinctUntilChanged:

import "rxjs/add/operator/distinctUntilChanged";

还有我在app.component.ts中的代码:

import { Component } from "@angular/core"; import { Router, NavigationEnd } from "@angular/router";
import { TranslateService } from "@ngx-translate/core";
import { GeoService } from "app/services/geo.service";
import { ArbeteService } from "app/services/arbete.service";
import { GoogleAnalyticsEventsService } from "app/services/google-analytics-events.service";

import * as _ from "lodash";

@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.scss"]
})
export class AppComponent {

        //Send pageview to Google Analytics
        router.events.distinctUntilChanged((previous: any, current: any) => {
        if (current instanceof NavigationEnd) {
            return previous.url === current.url;
        }
        return true;
    }).subscribe((x: any) => {
        googleAnalytics.emitVirtualPageView("Rekryteringsguiden", "Rekryteringsguiden");
    });

我在这里想念什么?

3 个答案:

答案 0 :(得分:3)

import { distinctUntilChanged } from 'rxjs/operators';

router.events.pipe(distinctUntilChanged((previous: any, current: any) => {})).subscribe();

答案 1 :(得分:1)

对于Angular 6,您的语法将在大多数RxJS运算符上更改。

要导入它,请使用此:

import { catchError } from 'rxjs/operators';

但是呼叫必须包裹在pipe中,如下所示:

    router.events
        .pipe(
            distinctUntilChanged((previous: any, current: any) => {
                if (current instanceof NavigationEnd) {
                    return previous.url === current.url;
                }
                return true;
            })).subscribe((x: any) => {
                googleAnalytics.emitVirtualPageView("Rekryteringsguiden", "Rekryteringsguiden");
});

答案 2 :(得分:0)

从rxjs 5.5导入操作符

import { distinctUntilChanged } from 'rxjs/operators';