【问题标题】:Property 'interval' does not exist on type 'typeof Observable'. Even though its imported“typeof Observable”类型上不存在属性“interval”。虽然是进口的
【发布时间】:2021-01-12 23:28:13
【问题描述】:

“typeof Observable”类型上不存在属性“interval”。即使它是进口的,我也不知道为什么它不起作用。我看过一些关于这个涉及角度 6 的帖子,但这是角度 11,所以我希望有人能帮我解决这个问题。

import { Component, OnInit } from '@angular/core';
import { TokenService } from '../../authentication/services/token.service';
import { Router } from '@angular/router';
import { HttpErrorResponse } from '@angular/common/http';
import { AdminService } from '../../authentication/services/admin.service';
import { CrudService } from '../services/crud.service';
import { Observable } from 'rxjs';
import { switchMap, startWith } from 'rxjs/operators';
@Component({
  selector: 'app-admin-dashboard',
  templateUrl: './admin-dashboard.component.html',
  styleUrls: ['./admin-dashboard.component.css'],
})
export class AdminDashboardComponent {
  constructor(
    private _token: TokenService,
    private _router: Router,
    private _admin: AdminService,
    private _crud: CrudService
  ) {}

  adminId: string;
  adminName: string;
  adminEmail: string;
  userCount$: Observable<any>;
  userCount: string;

  ngOnInit(): void {
    this._token.verifyToken().subscribe(
      (res) => {
        this.adminId = res.admin._id;
        localStorage.setItem('adminid', this.adminId);
        this._admin.getAdminById(this.adminId).subscribe((res) => {
          this.adminName = res.admin.name;
          this.adminEmail = res.admin.email;
          // this._crud.getUserCount().subscribe((count) => {
          //   this.userCount$ = count.count;
          // });
          this.userCount$ = Observable.interval(1000)
            .startsWith(0)
            .switchMap(() => {
              this._crud.getUserCount().subscribe((count) => {
                this.userCount = count.count;
              });
            });
        });
      },
      (err) => {
        if (err instanceof HttpErrorResponse) {
          if (err.status === 400) {
            this._router.navigate(['/login']);
          }
        }
      }
    );
  }
}

【问题讨论】:

  • 我也知道间隔没有被导入,但我之前已经导入了它,它仍然没有工作
  • 听起来您正在尝试混合“旧”方式和“新”方式。由于 rxjs 版本 6(我认为,事实检查),您需要导入各个运算符并将它们通过管道传递,而不是直接将它们附加到 observable。查找“lettable”或“pipeable”运算符。
  • 如果你导入interval并直接使用interval而不是Observable.interval会出现什么错误?
  • @Phix 我现在试试写

标签: javascript angular rxjs rxjs6 rxjs-observables


【解决方案1】:

试试这个:

import { interval } from 'rxjs';

....
interval(1000).startWith.... // Change Observable.interval to interval and import it from rxjs.

【讨论】:

  • 在我这样做之后,我得到 poperty 'startWith' 在类型 'Observable' 上不存在。
【解决方案2】:

现在解决了,只是忘了用管道

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2018-11-25
    • 2019-06-30
    • 1970-01-01
    相关资源
    最近更新 更多