gpt4 book ai didi

angular - 无法解析 AuthenticationService : ([object Object], 的所有参数?,[object Object])

转载 作者:太空狗 更新时间:2023-10-29 17:45:27 28 4
gpt4 key购买 nike

我遇到了下一个错误,无法理解如何解决它。

Can't resolve all parameters for AuthenticationService: ([object Object], ?, [object Object])

我几乎检查了这里的每个主题,并尝试了多种方法来解决它,但第二天仍然无法解决。

我试过像这样在 appService 中注入(inject)第一个 authService 但得到同样的错误

@Inject(forwardRef(() => AuthenticationService)) public authService: AuthenticationService

我已经检查了服务中的所有 DI 和导入顺序,在我看来一切都是正确的

所以,如果有人可以帮助我处理它,我将不胜感激。

Angular 4.0.0

授权服务

    import { Injectable } from '@angular/core';
import {Http, Headers, Response} from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {Observable} from 'rxjs/Rx';

import {AppServices} from "../../app.services";
import {Router} from "@angular/router";

@Injectable()
export class AuthenticationService {
public token: any;

constructor(
private http: Http,
private appService: AppServices,
private router: Router
) {
this.token = localStorage.getItem('token');
}

login(username: string, password: string): Observable<boolean> {
let headers = new Headers();
let body = null;
headers.append("Authorization",("Basic " + btoa(username + ':' + password)));

return this.http.post(this.appService.api + '/login', body, {headers: headers})
.map((response: Response) => {
let token = response.json() && response.json().token;
if (token) {
this.token = token;
localStorage.setItem('Conform_token', token);
return true;
} else {
return false;
}
});
}

logout(): void {
this.token = null;
localStorage.removeItem('Conform_token');
this.router.navigate(['/login']);
}
}

应用服务

import {Injectable} from '@angular/core';
import {Headers, Http, RequestOptions} from '@angular/http';
import {Router} from "@angular/router";
import {AuthenticationService} from "./auth/auth.service";

import 'rxjs/add/operator/toPromise';
import {Observable} from 'rxjs/Rx';

@Injectable()

export class AppServices {

api = '//endpoint/';

public options: any;
constructor(
private http: Http,
private router: Router,
public authService: AuthenticationService // doesn't work
// @Inject(forwardRef(() => AuthenticationService)) public authService: AuthenticationService // doesn't work either
) {
let head = new Headers({
'Authorization': 'Bearer ' + this.authService.token,
"Content-Type": "application/json; charset=utf8"
});
this.options = new RequestOptions({headers: head});
}

// ====================
// data services
// ====================

getData(): Promise<any> {
return this.http
.get(this.api + "/data", this.options)
.toPromise()
.then(response => response.json() as Array<Object>)
.catch((err)=>{this.handleError(err);})
}

应用模块

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import {BaseRequestOptions, HttpModule} from '@angular/http';

import { MaterialModule} from '@angular/material';
import {FlexLayoutModule} from "@angular/flex-layout";
import 'hammerjs';

import { routing, appRoutingProviders } from './app.routing';
import { AppServices } from './app.services';
import {AuthGuard} from "./auth/auth.guard";
import {AuthenticationService} from "./auth/auth.service";

import {AppComponent} from './app.component';
import {AuthComponent} from './auth/auth.component';
import {NotFoundComponent} from './404/not-found.component';
import { HomeComponent } from './home/home.component';

@NgModule({
declarations: [
AppComponent,
AuthComponent,
NotFoundComponent,
HomeComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
HttpModule,
routing,
MaterialModule,
FlexLayoutModule
],
providers: [AppServices, AuthGuard, AuthenticationService],
bootstrap: [AppComponent]
})
export class AppModule { }

最佳答案

您在 AppServicesAuthenticationService 之间存在循环依赖 - 这对于 Angular 使用的构造函数注入(inject)是不可能的。

要变通,您可以使用

export class AuthenticationService {
public token: any;
appService: AppServices;
constructor(
private http: Http,
// private appService: AppServices,
injector:Injector;
private router: Router
) {
setTimeout(() => this.appService = injector.get(AppServices));
this.token = localStorage.getItem('token');
}

另见 DI with cyclic dependency with custom HTTP and ConfigService

要避免 setTimeout,您还可以从 AppService 的构造函数中设置 AuthenticationService.appService(或相反)

关于angular - 无法解析 AuthenticationService : ([object Object], 的所有参数?,[object Object]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43098092/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com