简体   繁体   English

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

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

I faced with next error and cannot understand how to resolve it. 我遇到了下一个错误,无法理解如何解决它。

Can't resolve all parameters for AuthenticationService: ([object Object], ?, [object Object]) 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])

I've checked almost every topic here and have tried multiple ways to resolve it but still can't beat it already second day. 我已经检查了几乎每个主题,并尝试了多种方法来解决它,但仍然无法在第二天击败它。

I have tried to inject first authService in appService like this but getting same error 我试图像这样在appService中注入第一个authService但是得到了同样的错误

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

I have checked all DI and order of imports inside services and it seems to me everything is correct 我检查了所有DI和服务内部的导入顺序,在我看来一切都是正确的

So I appreciate if somebody could help me deal with it. 如果有人可以帮我处理它,我很感激。

Angular 4.0.0 Angular 4.0.0

AuthService AuthService

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

App Services 应用服务

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

App Module 应用模块

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 { }

You have a circular dependency between AppServices and AuthenticationService - that's not possible with constructor injection like Angular uses. AppServicesAuthenticationService之间存在循环依赖关系 - 这与Angular使用的构造函数注入无法实现。

To work around you can use 你可以使用

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

See also DI with cyclic dependency with custom HTTP and ConfigService 另请参阅DI与自定义HTTP和ConfigService的循环依赖关系

To avoid setTimeout you can also set AuthenticationService.appService from the constructor of AppService (or the other way around) 要避免使用setTimeout您还可以从AppService的构造函数中设置AuthenticationService.appService (或者相反)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法解析...的所有参数([对象对象],[对象对象],?,?) - Can't resolve all parameters for … ([object Object],[object Object], ?, ?) 无法解析HttpXsrfCookieExtractor的所有参数:(?,[对象对象],[对象对象]) - Can't resolve all parameters for HttpXsrfCookieExtractor: (?, [object Object], [object Object]) 无法解析组件的所有参数:(?, [object Object], [object Object]) - Can't resolve all parameters for Component: (?, [object Object], [object Object]) 无法解析PlatformService的所有参数:([对象对象],?,[对象对象]) - Can't resolve all parameters for PlatformService: ([object Object], ?, [object Object]) 无法解析 ToastrService 的所有参数:(?, [object Object], [object Object], [object Object], [object Object]) - Can't resolve all parameters for ToastrService: (?, [object Object], [object Object], [object Object], [object Object]) 错误:无法解析 LoginPage 的所有参数:([object Object], [object Object], [object Object], ?) - Error: Can't resolve all parameters for LoginPage: ([object Object], [object Object], [object Object], ?) 错误:无法解析&#39;Component&#39;的所有参数(?,[object Object]) - Error in: Can't resolve all parameters for 'Component' (?, [object Object]) 无法解析ObservableDataSource的所有参数:([object Object] ,?) - Can't resolve all parameters for ObservableDataSource: ([object Object], ?) 无法解析AnalysisComponent的所有参数:([[Object Object],?,?,[ - Can't resolve all parameters for AnalysisComponent: ([object Object], ?, ?, [ 错误:无法解析主页的所有参数:([object Object],?) - Error: Can't resolve all parameters for HomePage: ([object Object], ?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM