StaticInjectorError-填充下拉列表

时间:2019-01-18 17:32:31

标签: angular api asp.net-core angular6

Angular v6.1.10 | ASP.Net Core v2.2.102


我正在尝试从API提取数据以在Angular应用程序中填充下拉列表。

我在控制台上收到以下错误和警告:

警告

  

WebSocket连接到   'ws:// localhost:5000 / sockjs-node / 050 / xwusnroj / websocket'失败:   建立连接之前,请关闭WebSocket。   WebSocketTransport.close @ sockjs.js:2998

错误

  

ERROR错误:未捕获(承诺):错误:   StaticInjectorError(AppModule)[PortoService-> Http]:
  StaticInjectorError(平台:核心)[PortoService-> Http]:       NullInjectorError:没有Http的提供程序!

如下图所示:

Warning and Error

运行Karma调试器时,我得到以下规格列表:

  

CounterComponent应该显示标题,其开头应为count 0,   然后在单击FishFormComponent创建时增加1   应该创建PortoService

enter image description here

我不确定为什么会出现这些错误。任何帮助表示赞赏!


服务

porto.services.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class PortoService {

  constructor(private http: Http) { }

  getPortos() {
    return this.http.get('/api/portos')
      .map(res => res.json());
  }
}

应用

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { NavMenuComponent } from './nav-menu/nav-menu.component';
import { HomeComponent } from './home/home.component';
import { CounterComponent } from './counter/counter.component';
import { FetchDataComponent } from './fetch-data/fetch-data.component';
import { FishFormComponent } from './fish-form/fish-form.component';
import { PortoService } from './services/porto.service';

@NgModule({
  declarations: [
    AppComponent,
    NavMenuComponent,
    HomeComponent,
    CounterComponent,
    FetchDataComponent,
    FishFormComponent
  ],
  imports: [
    BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
    HttpClientModule,
    FormsModule,
    RouterModule.forRoot([
      { path: '', component: HomeComponent, pathMatch: 'full' },
      {path: 'fishes/new', component: FishFormComponent},
      { path: 'counter', component: CounterComponent },
      { path: 'fetch-data', component: FetchDataComponent },
    ])
  ],
  providers: [
    { provide: 'ORIGIN_URL', useValue: location.origin },
    PortoService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

表格

fish-form.components.ts

import { PortoService } from './../services/porto.service';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-fish-form',
  templateUrl: './fish-form.component.html',
  styleUrls: ['./fish-form.component.css']
})
export class FishFormComponent implements OnInit {
  portos;

  constructor(private PortoService: PortoService) { }

  ngOnInit() {
    this.PortoService.getPortos().subscribe(portos => {
      this.portos = portos;
      console.log("PORTOS", this.portos);
    });
  }

}

fish-form.component.ts

<h1>Adiciona um Peixe Novo</h1>
<form>
  <div class="form-group">
    <label for="porto">Porto</label>
    <select id="porto" class="form-control">
      <option value=""></option>
      <option *ngFor="let p of portos" value="{{ p.id }}">{{ p.nome }}</option>
    </select>
  </div>
  <div class="form-group">
    <label for="especie">Especie</label>
    <select id="especie" class="form-control"></select>
  </div>
</form>

1 个答案:

答案 0 :(得分:2)

按照@ABOS的建议通过将porto.services.ts中的导入更新为:

import { HttpClient } from '@angular/common/http';

并进行更改:

constructor(private http: Http)

constructor(private http: HttpClient)

HttpClient.get()自动应用res.json()并返回Observable<HttpResponse<string>>时,由于不需要使用map方法,因此我删除了.map(res => res.json());