关于angular:Cannot-instantiate-cyclic-dependency

88次阅读

共计 552 个字符,预计需要花费 2 分钟才能阅读完成。

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 曾经解决了这个问题。不必去掉路由的局部;

正文完
 0