我在Angular 5项目上有以下循环依赖性错误,我不明白为什么。 我从token.service.ts中删除路由器依赖项时修复了错误,但我需要它。我在代码中尝试做的是在应用程序初始化期间调用一个方法 这就是我所拥有的:
app.modules.ts
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { SearchEngineModule } from './search-engine/search-engine.module';
import { AppLoadModule } from './app-load/app-load.module';
import { AppLoadService } from './app-load/app-load.service';
import { AppComponent } from './app.component';
import { SearchService } from './search-engine/search.service';
import { AppRoutingModule } from './app-routing.module';
import { Routes, RouterModule } from '@angular/router';
import { SearchListComponent } from './search-engine/search-list/search-list.component';
import { TokenService } from './token.service';
const routes: Routes = [
{
path: 'searchList',
component: SearchListComponent
}
];
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
HttpModule,
AppLoadModule,
SearchEngineModule,
RouterModule.forRoot(routes, { useHash: true })
],
providers: [ TokenService , AppLoadService],
bootstrap: [ AppComponent ]
})
export class AppModule { }
APP-load.module.ts
import { NgModule, APP_INITIALIZER} from '@angular/core';
import { AppLoadService } from './app-load.service';
import { CommonModule } from '@angular/common';
export function init_app(appLoadService: AppLoadService) {
return () => appLoadService.initializeApp();
}
@NgModule({
imports: [
CommonModule
],
providers: [
{ provide: APP_INITIALIZER,
useFactory: init_app,
deps: [AppLoadService],
multi: true
}
],
declarations: []
})
export class AppLoadModule { }
APP-load.service.ts
import { Injectable } from '@angular/core';
import { TokenService } from '../token.service';
@Injectable()
export class AppLoadService {
constructor(private tokenService: TokenService) { }
initializeApp(): Promise<any> {
return new Promise((resolve, reject) => {
setTimeout(() => {
this.tokenService.obtainAccessToken();
resolve();
}, 3000);
});
}
}
token.service.ts
import { Injectable } from '@angular/core';
import { Router } from "@angular/router";
import { Cookie } from 'ng2-cookies';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
@Injectable()
export class TokenService {
constructor(private router: Router, private _http: Http) { }
obtainAccessToken() {
// some code
this.router.navigate(['/']);
}
}
有人可以提出任何建议吗? 提前致谢