问题描述: 当使用ASP.NET Web API和Angular 8构建应用程序时,发现更新不起作用。
解决方法:
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable()
export class MyService {
private apiUrl = 'http://localhost:5000/api/';
constructor(private http: HttpClient) { }
getData() {
const headers = new HttpHeaders({
'Content-Type': 'application/json',
'Accept': 'application/json'
});
return this.http.get(this.apiUrl + 'data', { headers: headers });
}
}
import { Component, OnInit } from '@angular/core';
import { MyService } from './my.service';
@Component({
selector: 'app-my-component',
template: `
{{ item.name }}
`,
providers: [MyService]
})
export class MyComponent implements OnInit {
public data: any[];
constructor(private service: MyService) { }
ngOnInit() {
this.service.getData().subscribe(
(res: any[]) => {
this.data = res;
},
(error) => {
console.log(error);
}
);
}
}
通过以上解决方法,应该能够解决ASP.NET Web API和Angular 8更新不起作用的问题。如果仍然存在问题,请检查浏览器的开发者工具中的网络请求和控制台输出,以获取更多的错误信息。