angular - “请求”类型的参数不可分配给“HttpRequest”类型的参数'
问题描述
我是新来的。请保持温和,说外行。
在以下打字稿代码中,我的方法 sendRequest() 中出现此错误...
“Request”类型的参数不能分配给“HttpRequest”类型的参数。“请求”类型中缺少属性“正文”。
我附上了邮件的图片。请问有人知道如何解决这个问题吗?谷歌运气不好。
import { Movie } from "./movie.model";
import { Injectable } from '@angular/core';
import { RequestMethod, Request, Response } from '@angular/http';
import { HttpClient } from '@angular/common/http';
import { Observable } from "rxjs/Observable";
import "rxjs/add/operator/map";
import { Filter } from "./configClasses.repository";
import { Studio } from "./studio.model";
const studiosUrl = "/api/studios";
const moviesUrl = "/api/movies";
@Injectable()
export class Repository {
private filterObject = new Filter();
private movieData: Movie;
constructor(private http: HttpClient) {
//this.filter.category = "drama";
this.filter.related = true;
this.getMovies(true);
}
getMovie(id: number) {
this.sendRequest(RequestMethod.Get, moviesUrl + "/" + id)
.subscribe(response => { this.movie = response.json(); });
//console.log("Movie Data Requested");
}
getMovies(related = false) {
let url = moviesUrl + "?related=" + this.filter.related;
if (this.filter.category) {
url += "&category=" + this.filter.category;
}
if (this.filter.search) {
url += "&search=" + this.filter.search;
}
this.sendRequest(RequestMethod.Get, url)
.subscribe(response => this.movies = response);
}
getStudios() {
this.sendRequest(RequestMethod.Get, studiosUrl)
.subscribe(response => this.studios = response);
}
createMovie(mov: Movie) {
let data = {
name: mov.name, category: mov.category,
description: mov.description, price: mov.price,
studio: mov.studio ? mov.studio.studioId : 0
};
this.sendRequest(RequestMethod.Post, moviesUrl, data)
.subscribe(response => {
mov.movieId = response;
this.movies.push(mov);
});
}
createMovieAndStudio(mov: Movie, stu: Studio) {
let data = {
name: stu.name, city: stu.city, state: stu.state
};
this.sendRequest(RequestMethod.Post, studiosUrl, data)
.subscribe(response => {
stu.studioId = response;
mov.studio = stu;
this.studios.push(stu);
if (mov != null) {
this.createMovie(mov);
}
});
}
replaceMovie(mov: Movie) {
let data = {
name: mov.name, category: mov.category,
description: mov.description, price: mov.price,
studio: mov.studio ? mov.studio.studioId : 0
};
this.sendRequest(RequestMethod.Put, moviesUrl + "/" + mov.movieId, data)
.subscribe(response => this.getMovies());
}
replaceStudio(stu: Studio) {
let data = {
name: stu.name, city: stu.city, state: stu.state
};
this.sendRequest(RequestMethod.Put,
studiosUrl + "/" + stu.studioId, data)
.subscribe(response => this.getMovies());
}
private sendRequest(verb: RequestMethod, url: string,
data?: any): Observable<any> {
return this.http.request(new Request({
method: verb,
url: url,
body: data
})).map(response => {
return response.headers.get("Content-Length") != "0"
? response.json() : null;
});
}
updateMovie(id: number, changes: Map<string, any>) {
let patch = [];
changes.forEach((value, key) =>
patch.push({ op: "replace", path: key, value: value }));
this.sendRequest(RequestMethod.Patch, moviesUrl + "/" + id, patch)
.subscribe(response => this.getMovies());
}
movie: Movie;
movies: Movie[];
studios: Studio[] = [];
get filter(): Filter {
return this.filterObject;
}
deleteMovie(id: number) {
this.sendRequest(RequestMethod.Delete, moviesUrl + "/" + id)
.subscribe(response => this.getMovies());
}
deleteStudio(id: number) {
this.sendRequest(RequestMethod.Delete, studiosUrl + "/" + id)
.subscribe(response => {
this.getMovies();
this.getStudios();
});
}
}
解决方案
像这样在“@angular/http”中使用“Http”(不推荐使用这种方式)
从 '@angular/http' 导入 { RequestMethod, Http, Request, Response };
不要从 '@angular/common/http' 导入 { HttpClient };
将构造函数更改为:
constructor(private http: Http) {
//this.filter.category = "drama";
this.filter.related = true;
this.getMovies(true);
}
来源:Angular.io -> api -> http -> 请求
或不推荐您可以这样做:
...构造函数(私有http:HttpClient)...
public get<T>(url: string, headers: object) {
return this.http.get<T>(url, headers);
}
public post<T>(url: string, body: any, headers: object) {
return this.http.post<T>(url, body, headers);
}
推荐阅读
- php - Wordpress PHP:将帖子与类别存档上的自定义帖子类型结合起来
- c++ - CNTK:->Forward 或 ->Evaluate crash 在某些 PC 上,而不是在其他 PC 上
- python - Python Klein - 如何将 Excel 文件作为附件返回?
- java - MaterialAnimatedSwitch 保存状态
- javascript - 如何在正在测试的父对象中模拟子函数的输出?
- facebook - Facebook 与自定义图像单页应用程序共享
- javascript - .each() 将太多项目推送到数组
- azure - Azure MySQL数据库错误客户端和服务器无法通信,因为它们没有通用算法
- c++ - 空 grpc 客户端通道
- flutter - Flutter:跟踪应用打开的次数