我是新来的。请保持温柔并说外行。
在以下打字稿代码中,我的方法sendRequest()中遇到此错误...
“请求”类型的参数不能分配给“ 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();
});
}
}
答案 0 :(得分:0)
下面应该可以工作:
private sendRequest(verb: RequestMethod, url: string,
data?: any): Observable<Request> {
return this.http.request(new Request({
method: verb,
url: url,
body: data
})).map(response => {
return response.headers.get("Content-Length") != "0"
? response.json() : null;
});
}
答案 1 :(得分:0)
在“ @ 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 -> Request
或者不喜欢,您可以这样做:
...构造函数(私有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);
}
答案 2 :(得分:0)
有两种方法可以解决此问题,如Vancho上面提到的那样具有特定于它的get和post方法,让您尝试以下代码:
private sendRequest(verb: RequestMethod, url: string,
data?: any): Observable<any> {
let method: string;
switch (verb) {
case RequestMethod.Get:
method = "GET";
break;
case RequestMethod.Post:
method = "POST";
break;
}
return this.http.request(method, url, { body: data }).map(response => {
return response.headers.get("Content-Length") != "0"
? response.json() : null;
});
}
我不建议您采用这条路线,因为它不可扩展,您应该具有特定于get / post / delet方法的代码,因此您可以使用2-3种方法:
sendGetRequest和sendPostRequest分别具有http.get和http.post。