Я использую службы http в angular У меня есть конструктор:
import { HttpClientModule,HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-posts',
templateUrl: './posts.component.html',
styleUrls: ['./posts.component.css']
})
export class PostsComponent implements OnInit {
posts:any[];
constructor( private http:HttpClient) {
http.get('http://jsonplaceholder.typicode.com/posts').
subscribe(response=>{
this.posts=<any>response;
})
}
ngOnInit(): void {
}
}
HTml часть:
<ul class="list-group">
<li *ngFor="let post of posts" class="list-group-item">{{post.title}}</li>
</ul>
Однако в консоли я получаю ошибку как "Uncaught TypeError: Невозможно прочитать свойство 'id' из undefined". Попытался несколькими способами, но все еще получает эту ошибку.
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { interval } from 'rxjs';
import {NgForm, FormsModule, ReactiveFormsModule} from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { Block1Component } from './block1/block1.component';
import { Block2Component } from './block2/block2.component';
import { HomeComponent } from './home/home.component';
import { PostsComponent } from './posts/posts.component';
import { HttpClientModule,HttpClient } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent,
Block1Component,
Block2Component,
HomeComponent,
PostsComponent
],
imports: [
BrowserModule,
HttpClient,
AppRoutingModule,
HttpClientModule,
FormsModule,
ReactiveFormsModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Добавлен модуль приложения. проблема в том, что я снова получаю сообщение об ошибке как
core.js:35929 Uncaught TypeError: Cannot read property 'id' of undefined
at registerNgModuleType (core.js:35929)
at core.js:35947
at Array.forEach (<anonymous>)
at registerNgModuleType (core.js:35943)
at new NgModuleFactory$1 (core.js:36105)
at compileNgModuleFactory__POST_R3__ (core.js:41895)
at PlatformRef.bootstrapModule (core.js:42260)
at Module../src/main.ts (main.ts:12)
at __webpack_require__ (bootstrap:79)
at Object.0 (main.ts:13)