angularjs - 角度获取和发布方法在角度 9 中不起作用
问题描述
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);
}
}
内存数据库服务
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;
}
- 在上面的代码中,我尝试同时使用 get 方法和 post 方法
- 我正在使用角度 9<br/
- 我没有使用后面的 API,而是使用有角度的内置 inMermoryDbService
- 在 colsoe 中:加载资源失败:服务器响应状态为 404(未找到)
- 在 colsoe 中:拒绝加载图像“http://localhost:4200/favicon.ico”,因为它违反了以下内容安全策略指令:“default-src 'none'”。请注意,'img-src' 没有显式设置,因此 'default-src' 用作后备。
- 加载资源失败:服务器响应状态为 404(未找到)我没有使用后台 API,而是使用 angular 内置 inMermoryDbService
(7)IN VSCODE 错误:无法编译入口点 @angular/ common/http/testing (es2015 as esm2015) 由于编译错误:node_modules/@angular/common/http/http.d.ts:2823:22 - 错误 NG6002:出现在 HttpClientTestingModule 的 NgModule.imports 中,但不能解析为 NgModule 类。
这可能意味着声明 HttpClientModule 的库 (@angular/common/http) 没有被 ngcc 正确处理,或者与 Angular Ivy 不兼容。检查是否有更新版本的库可用,如果有则更新。还可以考虑与图书馆的作者核实,看看图书馆是否应该与 Ivy 兼容。
解决方案
推荐阅读
- spring-boot - 有没有办法在一个地方记录所有@ExceptionHandler 的异常
- powershell - 使用 PowerShell 通过 VPN 发现所有有效 IP
- sed - GNU sed 长替换
- docker - 尝试使用我在 Docker 中构建的映像在 Kubernetes 中部署 Jenkins 时出现 CreateContainerError
- android - MotionLayout 中的 ConstraintSets 没有复制粘贴属性
- java - Intellij 无法读取资源子文件夹中的图像
- powershell - powershell 从类型目录中选择所有对象
- c# - 版本高于项目的Mysql(C# WPF)
- angular - 当我的所有组件都@import“变量”时,如何向我的 SCSS 驱动的 Angular 应用程序添加对多个主题的支持?
- reactjs - 从同一文件导入简单函数并破坏测试时调用的 CreateAsyncThunk 函数