javascript - 函数更新联系人未定义,虽然已定义
问题描述
我这里有角码:
@Component({
selector: 'app-detail-view',
templateUrl: './detail-view.component.html',
styleUrls: ['./detail-view.component.css']
})
export class DetailViewComponent implements OnInit {
detailLable:string;
contactId: number;
contact:Contact = null;
constructor(
private contactService: ContactService,
private route: ActivatedRoute,
private location: Location
) {}
ngOnInit(): void {
this.contactId = +this.route.snapshot.paramMap.get('id');
if(this.contactId == 0){
this.contact = {id : null, firstName : null, surname : null, email: null, phone: null};
this.detailLable = 'New contact'
} else {
this.contactService.getContact(this.contactId).subscribe(contact => this.contact = contact);
this.detailLable = 'Edit contact with id :' + this.contactId;
}
}
updateContact( contact: Contact): void{
this.contact = contact;
this.detailLable = "Edit contact with id " +contact.id
}
goBack(): void {
this.location.back();
}
save():void {
// this.contact = this.contactService.saveContact(this.contact).subscribe(contact => this.contact = contact, complete: () => console.log(this.detailLable = this.contact.id));
this.contactService.saveContact(this.contact).subscribe(contact => updateContact(contact));
}
new() : void {
this.contact = {id : null, firstName : null, surname : null, email: null, phone: null};
}
}
当我调用保存时,它返回我未定义 updateContact 方法。有人可以给我一点,如何解决这个问题?看来,方法声明有问题。
解决方案
updateContact
在类上定义,所以this.updateContact
应该工作。
推荐阅读
- node.js - 节点 ts-jest spyOn 方法不匹配重载
- autodesk-forge - 在 MultiModel Autodesk Forge 中更改 DocumentBrowser 的文档
- google-cloud-build - 为什么 Google Cloud Build 创建中间容器?
- java - 如果延迟kafka手动提交偏移有什么影响
- reactjs - 我有一个左侧带有复选框的列表,我只想在新数组中添加选中的列表项
- javascript - 如何使用 React 动画切换类
- android - 如何修复 chrome 调试器端口转发中的断开链接
- java - SATO 打印机使用 Java 编程语言
- json - 如何在 mongodb 中匹配 json 对象中的特定值
- mysql - SQL查询按连续时间分组