将参数传递给服务构造函数?

Han*_*och 3 typescript angular-services angular

我正在尝试在组件中实例化服务。我收到这个错误

未捕获的错误:无法解析 AuthenticationService 的所有参数:(?, [object Object], ?)。

这是我的服务

     @Injectable()
    export class AuthenticationService {

      protected basePath = 'http://localhost:8080/rest';
      public defaultHeaders: Headers = new Headers();
      public configuration: Configuration = new Configuration();

      constructor(protected http: Http, @Optional() @Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
        if (basePath) {
          this.basePath = basePath;
        }
        if (configuration) {
          this.configuration = configuration;
          this.basePath = basePath || configuration.basePath || this.basePath;
        }
      }

    public login(authenticationkey?: string, body?: AuthenticationRequestHolder, extraHttpRequestParams?: any): Observable<AuthenticationBundle> {
        return this.loginWithHttpInfo(authenticationkey, body, extraHttpRequestParams)
          .map((response: Response) => {
            if (response.status === 204) {
              return undefined;
            } else {
              return FlexiCoreDecycl`enter code here`e.retrocycle(response.json()) || {};
            }
          });
      }

// More Code
Run Code Online (Sandbox Code Playgroud)

这是使用服务的组件

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  constructor(private _authenticationService: AuthenticationService) {
  }

  ngOnInit() {
    this._authenticationService
      .login('', {mail: 'admin@example.com', password: 'admin'})
      .subscribe(response => {
        console.log(response);
      },
        error => {
        console.log(error);
        });
  }
}
Run Code Online (Sandbox Code Playgroud)

这是角度模块

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [AuthenticationService],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

有人可以帮我如何正确连接它们吗?我是 angular 和 typescript 的新手,所以我会感谢一些解释和指针。

Han*_*och 5

可能还有另一种方法,但这是我解决它的方法。使用 FactoryBuider。这就是我的 app.module 喜欢的样子

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';

import {AppComponent} from './app.component';
import {LoginComponent} from './login/login.component';
import {AuthenticationService, BASE_PATH, Configuration} from '@hanoch/fc_client';
import {Http, HttpModule} from '@angular/http';
import {FormsModule} from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent
  ],
  imports: [
    BrowserModule,
    HttpModule,
    FormsModule
  ],
  providers: [
    Configuration,
    {
      provide: AuthenticationService,
      useFactory: AuthenticationServiceFactory,
      deps: [Http]
    }
  ],
  bootstrap: [AppComponent]
})

export class AppModule {
}

export function AuthenticationServiceFactory(http: Http) {
  return new AuthenticationService(http, 'http://localhost:8080/rest', Configuration);
}
Run Code Online (Sandbox Code Playgroud)