Cannot instantiate cyclic dependency

angualr:8.0.6
产生的原因是,旧的版本会出现循环引用依赖错误;
出现场景:
user.service.ts

import {Injectable} from '@angular/core';  
import {of} from 'rxjs';  
import {ActivatedRoute, Router} from '@angular/router';  
import {tap} from 'rxjs/operators';  
  
@Injectable({  
  providedIn: 'root'  
})  
export class UserService {  
  
  constructor(  
    private router: Router,  
 private route: ActivatedRoute  
  ) {  
  }  
  
  loginByToken() {  
    console.log('init start');  
  
 return of('111').pipe(tap(() => {  
      setTimeout(() => {  
        this.router.navigateByUrl('/login');  
 }, 2000);  
 }));  
 }  
  
}

解决方案是,去掉下面的依赖项

private router: Router,  
private route: ActivatedRoute

angular9 和 angular10 已经解决了这个问题。不用去掉路由的部分;

阅读 195

推荐阅读