0
코드 조각이 있는데 작동하지 않습니다. 왜 그 코드를 관리 할 수 없었습니다. 도와주세요.typescript의 함수 안에 값을 할당하십시오.
@Injectable()
export class PersonService {
private person: Person = new Person();
...
getPerson(name: string): void {
const url = `${this.personsUrl}/getByName/${name}`;
this.http.get<Person>(url).toPromise().then(r => {
console.log(r); // {id: 1, name: "PersonName"}
console.log(r.name); // PersonName
console.log(r.id); // 1
this.person.id = r.id;
this.person.name = r.name;
});
console.log(this.person) // Person {} <--- why?
}
}
나는 그것을 돌려, then()
기능 내부 Person
객체를 생성 등 Promise
문서의 복사본을 만드는 시도를 반환하려고 노력하지만, 여전히 어떤 이유로 작동하지 않습니다.
고맙습니다! 도움이되었습니다. 그러나 '무효'대신 '약속'을 돌려 주어야한다. –
@VitaliKuzmin 함수의 반환 유형이 변경되지 않지만 여전히 'void'입니다. – James
반환 형식을 변경하기 전까지는 오류없이 컴파일하지 않으려했습니다. 그리고 IDE는이 문제를 지적하고있었습니다. –