简体   繁体   中英

Property 'interval' does not exist on type 'typeof Observable'. Even though its imported

Property 'interval' does not exist on type 'typeof Observable'. Even though its imported in and I dont know why it wont work. Ive seen some posts about this involving angular 6 but this is angular 11 so I hope someone can help me figure this out.

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']);
          }
        }
      }
    );
  }
}

Try this:

import { interval } from 'rxjs';

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

Its solved now I just forgot to use pipe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM