【问题标题】:Angular Can't resolve all parametersAngular 无法解析所有参数
【发布时间】:2017-10-25 20:40:36
【问题描述】:

嗨,我有这个来自 Angular Can't resolve all parameters for RoomService: (?). 的错误,我有这两个文件,是服务。

room.service.ts

import { Injectable } from '@angular/core';
import { ResourceService } from './resource.service';
import { Http } from '@angular/http';
import { CONFIG as APP_CONSTANTS } from '../config/config';

@Injectable()
export class RoomService extends ResourceService {

  constructor(http) {
    super(http);
  }

}

app.module.ts

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

// Angular Material
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialComponents } from '../ngcomponents/material.component';

import { AppRoutingModule } from './app-routing.module';

// Components
import { AppComponent } from './app.component';
import { RoomlistComponent } from './components/roomlist/roomlist.component';

// services
import { HttpModule } from '@angular/http';
import { RoomService } from './services/room.service';

@NgModule({
  declarations: [
    AppComponent,
    RoomlistComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpModule,
    BrowserAnimationsModule,
    MaterialComponents
  ],
  providers: [RoomService],
  bootstrap: [AppComponent]
})
export class AppModule { }

我不知道问题是什么所以。

【问题讨论】:

    标签: angular angular-services


    【解决方案1】:

    构造函数应具有Http 类型的http 参数。通过 Dependency Resolver 了解的内容,他应该创建一个 Http 注入器的令牌。

    constructor(http: Http) {
       super(http);
    }
    

    【讨论】:

      【解决方案2】:

      Angular 不知道要传递什么,因为 http 没有类型注释

      constructor(http) {
      

      如果你改成

      constructor(http:Http) {
      

      并将HttpModule 添加到AppModuleimports: [...],它应该可以工作。 您还需要为 Http 添加一个 TypeScript 导入。

      另见

      【讨论】:

        【解决方案3】:

        在你的 roomService 中,你的 DI 应该是

         constructor(http: Http) {
            super(http);
          }
        

        【讨论】:

          猜你喜欢
          • 2017-10-05
          • 1970-01-01
          • 1970-01-01
          • 2018-12-06
          • 2020-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多