类型 'void' 不可分配给类型 'Contact'
Type 'void' is not assignable to type 'Contact'
detail.component.ts
contact: Contact;
constructor( private contactService: LocalStorageService) {}
ngOnInit(): void {
this.contact = this.contactService
.getContact(815);
}
contact.service.ts
getContact(id: number) {
var contact =
JSON.parse(LocalStorage.getItem("contacts")).filter((cntct) => {
return cntct.id == id
});
}
Error: Type 'void' is not assignable to type 'Contact'.
这里,在 url 我有 /detail/"clicked-contacts-id-number"
我想获取该联系人 ID 的联系人数据。
您没有通过获取联系方式返回联系信息
getContact(id: number) {
var contact =
JSON.parse(LocalStorage.getItem("contacts")).filter((cntct) => { return
cntct.id == id });
return contact //add this line
}
您尚未在 contact.service.ts 中定义 return 类型的 getContact() 函数。
试试这个..
getContact(id: number): 联系人 {
您忘记了 return 联系人。您可以像这样简化代码
getContact(id: number) {
return JSON.parse(LocalStorage.getItem("contacts")).filter((cntct) => {
return cntct.id == id });
}
detail.component.ts
contact: Contact;
constructor( private contactService: LocalStorageService) {}
ngOnInit(): void {
this.contact = this.contactService
.getContact(815);
}
contact.service.ts
getContact(id: number) {
var contact =
JSON.parse(LocalStorage.getItem("contacts")).filter((cntct) => {
return cntct.id == id
});
}
Error: Type 'void' is not assignable to type 'Contact'.
这里,在 url 我有 /detail/"clicked-contacts-id-number"
我想获取该联系人 ID 的联系人数据。
您没有通过获取联系方式返回联系信息
getContact(id: number) {
var contact =
JSON.parse(LocalStorage.getItem("contacts")).filter((cntct) => { return
cntct.id == id });
return contact //add this line
}
您尚未在 contact.service.ts 中定义 return 类型的 getContact() 函数。
试试这个..
getContact(id: number): 联系人 {
您忘记了 return 联系人。您可以像这样简化代码
getContact(id: number) {
return JSON.parse(LocalStorage.getItem("contacts")).filter((cntct) => {
return cntct.id == id });
}