错误 TS2339: 类型'Observable <Event> '上不存在属性'filter'

4

我的应用程序出现错误 错误 TS2339:在类型“Observable”上不存在属性“filter” 我尝试导入各种东西,但没有起作用。 我的组件 ts 文件

import { Component, OnInit } from '@angular/core';
  import {
  Router,
  ActivatedRoute,
  NavigationEnd,
  Params,
  PRIMARY_OUTLET
} from '@angular/router';
import 'rxjs/add/operator/filter';

  ngOnInit() {
    const ROUTE_DATA_BREADCRUMB: string = 'breadcrumb';
    //subscribe to the NavigationEnd event
    this.router.events
      .filter(event => event instanceof NavigationEnd)
      .subscribe(event => {
        //set breadcrumbs
        let root: ActivatedRoute = this.activatedRoute.root;
        this.breadcrumbs = this.getBreadcrumbs(root);
        console.log(this.breadcrumbs);
      });
  }

我的 package.json

  "dependencies": {
    "@agm/core": "^1.0.0-beta.5",
    "@angular/animations": "^6.1.8",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "bootstrap3": "^3.3.5",
    "core-js": "^2.5.4",
    "jquery": "^3.3.1",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26"
  },

我不知道出了什么问题,但是出现了这个错误 错误 TS2339:在类型为“Observable”的对象上不存在属性“filter”

1个回答

6

愚蠢的错误只需要使用管道即可解决

ngOnInit() {
    this.router.events.pipe(
        filter(event => event instanceof NavigationEnd)
    ).subscribe(event => {
    //set breadcrumbs
    let root: ActivatedRoute = this.activatedRoute.root;
    this.breadcrumbs = this.getBreadcrumbs(root);
    console.log(this.breadcrumbs);
  });
}

并且导入

import { filter } from 'rxjs/operators';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接