首页 > 解决方案 > 类型“订阅”不可分配给类型“订阅”

问题描述

构建我的 JHipster Angular 项目时出现此错误。它发生在命令之后yarn start

import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Subscription } from 'rxjs/Subscription';
import { JhiEventManager, JhiParseLinks, JhiAlertService } from 'ng-jhipster';
import { ITEMS_PER_PAGE, Principal } from '../../shared';
import { Observable } from 'rxjs/Observable';
....
private subscription: Subscription;
    private eventSubscriber: Subscription;
...
 ngOnInit() {
        this.subscription = this.route.params.subscribe((params) => {
            this.load(params['id']);
        });
}


> TS90010: Type 'Subscription' is not assignable to type 'Subscription'.
> Two different types with this name exist, but they are unrelated.

但它以前工作没有问题。我不知道为什么会这样。

标签: angulartypescriptrxjsyarnpkg

解决方案


不要直接回答您的问题,而是不要使用订阅对象跟踪您的所有订阅,只需使用一个主题并执行直到。这是在 Angular 中管理 rxjs 订阅的推荐方式。

finalise = new Subject<void>();

this.route.params.subscribe((params) => {
  this.load(params['id']);
}).pipe(takeUntil(this.finalise)); // No need to unsubscribe if finalise emits.

ngOnDestroy() {
  this.finalise.next();
  this.finalise.complete();
}

现在您只需要一个对象来管理组件中的所有订阅。


推荐阅读