html
<td>category:</td>
<td>
<input formControlName="category">
</td>
</tr>
<tr>
<td>writer:</td>
<td>
<input formControlName="writer">
</td>
</tr>
<td colspan="2">
<button [disable]="bookForm.invalid">Submit</button>
</td>
</table>
</form>
<h3>View</h3>
<table>
<tr>
<th>Id</th><th>Name</th><th>category</th><th>writer</th>
</tr>
<tr *ngFor="let book of allBooks | async">
<td>{{book.id}}</td>
<td>{{book.name}}</td>
<td>{{book.category}}</td>
<td>{{book.wrter}}</td>
</tr>
</table>
组件
import { Component } from '@angular/core';
import { Book } from './Book';
import { BookService } from './book.service';
import {FormGroup,FormBuilder,Validators} from '@angular/forms';
import { Observable } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'sam';
dataSaved = false;
bookFrom : FormGroup;
allBooks : Observable<Book[]>;
constructor(private formBuilder : FormBuilder, private bookService : BookService){}
ngOnInit(): void {
this.bookFrom=this.formBuilder.group({
id : ['', [Validators.required]],
name : ['', [Validators.required]],
category : ['', [Validators.required]],
writer : ['', [Validators.required]],
})
this.getSoftBooks();
}
getSoftBooks(){
this.allBooks=this.bookService.getBooksFromStore();
}
onFromSubmit(){
this.dataSaved=false;
let book=this.bookFrom.value;
this.createBooks(book);
this.bookFrom.reset();
}
createBooks(book : Book){
this.bookService.createBook(book).subscribe(book=>{
this.dataSaved=true;
this.getSoftBooks();
})
}
}
模块
import { BrowserModule } from '@angular/platform-browser';
import {HttpClientModule} from '@angular/common/http'
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import {BookService} from './book.service'
import {InMemoryWebApiModule} from 'angular-in-memory-web-api'
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
InMemoryWebApiModule,
ReactiveFormsModule
],
providers: [BookService],
bootstrap: [AppComponent]
})
export class AppModule { }
export interface Book{
id: number;
name: string;
category : string;
writer: String;
}
服务
import { Injectable } from '@angular/core';
import{HttpClient, HttpHeaders} from'@angular/common/http';
import {Observable} from 'rxjs';
import {Book} from './Book';
@Injectable({
providedIn: 'root'
})
export class BookService {
bookUrl="/api/books";
constructor(private http : HttpClient){}
getBooksFromStore():Observable<Book[]>{
return this.http.get<Book[]>(this.bookUrl);
}
createBook(book : Book): Observable<Book>{
let httpheader =new HttpHeaders()
.set('content-Type','application/Json');
let options={
header : httpheader
};
return this.http.post<Book>(this.bookUrl,options);
}
}
InMemoryDbService
import{InMemoryDbService} from 'angular-in-memory-web-api';
export class TestData implements InMemoryDbService{
createDb(){
let bookDetails=[
{id:100, name: 'anguular by saho' , category: 'angualr',writer:'chandan'},
];
return {books: bookDetails};
}
}
图书
export interface Book{
id: number;
name: string;
category : string;
writer: String;
}
这可能意味着声明HttpClientModule的库(@ angular / common / http)未被ngcc处理,或者与Angular Ivy不兼容。检查是否有较新版本的库,如果有,则进行更新。还可以考虑与该库的作者进行检查,以查看该库是否与Ivy兼容。