【问题标题】:Get or fetch API data using Angular (v5) with services and interface?使用带有服务和接口的 Angular (v5) 获取或获取 API 数据?
【发布时间】:2018-08-25 10:43:22
【问题描述】:

我正在使用 v 5 应用程序开发 Angular。我正在获取 API 并使用 https://angular.io/guide/http 文档。但我看不到任何东西。

如何解决文档提供的数据的这个问题?

我的文件夹结构:

https://imgur.com/A03KtDy

终端没有任何错误:

https://imgur.com/a/AEKxv

控制台没有问题:

https://imgur.com/JaPTvXZ

文件:app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { NavbarComponent } from './components/navbar/navbar.component';
import { HomeComponent } from './components/home/home.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { ConfigService } from './services/config.service';

const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'dashboard', component: DashboardComponent },
];

@NgModule({
  declarations: [
    AppComponent,
    NavbarComponent,
    HomeComponent,
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    )
    // other imports here
  ],
  providers: [ConfigService],
  bootstrap: [AppComponent]
})
export class AppModule { }

文件:config.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Config } from './config';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class ConfigService {

  constructor(private http: HttpClient) { }

  configUrl : 'http://localhost:3000/api/get';

  getConfig() {
    return this.http.get<Config>(this.configUrl)
    }
}

文件:dashboard.component.ts

import { Component, OnInit } from '@angular/core';
import { ConfigService } from '../../services/config.service';
import { Config } from '../../services/config';

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

  constructor(private configService : ConfigService) { }

  ngOnInit() { }

  config: Config;

showConfig() {
  this.configService.getConfig()
    // clone the data object, using its known Config shape
    .subscribe(data => this.config = { ...data });
}
}

文件:dashboard.component.html

<ul class="list-group" *ngFor="let config of configs">
  <li class="list-group-item list-group-item-dark">{{config.name}}</li>
</ul>

文件:config.ts

export interface Config {
    name : string
}

【问题讨论】:

  • 您可以发布您的 API 响应数据示例吗?
  • 您正在使用*ngFor="let config of configs",但您的仪表板组件中没有configs 属性。只有一个config 实例。
  • @Jeto 但在这种情况下可能会显示错误,但在上面的代码中是的,这似乎是个问题。
  • @Sh.Pavel 是的,在某些时候打电话给showConfig() 肯定也会有帮助:)
  • @Laiso ,我还没有尝试过这个项目 无论如何谢谢。 Angular v5 官方文档需要大量修改

标签: node.js angular mongoose


【解决方案1】:

我没有在您的代码中看到您调用 showConfig() 方法。

也许您忘记添加对 ngOnInit() 方法的调用?

 ngOnInit() { 
   this.showConfig();
}

【讨论】:

  • 是的,谢谢 我还需要在文件中添加:config.service.ts getConfig():Observable { return this.http.get(this. configUrl) } 文件:dashboard.component.ts ngOnInit() { this.showConfig() } 配置:配置;配置 = []; showConfig() { this.configService.getConfig() // 使用已知的 Config 形状克隆数据对象 .subscribe(data => this.configs = data ); .Angular v5 官方文档太具有误导性了,我想
  • @ManishVashisth 所以,如果你得到 configs[] 那么是的 - 它是,并在 ngOnInit 方法中调用你的方法,如我上面所示。
猜你喜欢
  • 2018-06-07
  • 2018-04-09
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
相关资源
最近更新 更多