【发布时间】:2017-06-13 12:36:05
【问题描述】:
我正在尝试使用 firebase 服务更新/编辑资源,并在更新/编辑后将其发送回列表组件。
this.firebaseService.updateListing(this.id, listing).then(function() {
this.router.navigate(['/listing/'+this.id]);
});
当我使用 foll 代码时,它可以工作,但我想弄清楚为什么上面的代码不起作用。任何帮助都将不胜感激。
this.firebaseService.updateListing(this.id, listing);
this.router.navigate(['/listings']);
以下是我使用第一种方法时遇到的错误:
Uncaught (in promise): TypeError: Cannot read property 'router' of undefined
以下是我的路线:
const appRoutes: Routes = [
{path:'', component:HomeComponent},
{path: 'listings', component:ListingsComponent},
{path: 'listing/:id', component:ListingComponent},
{path: 'edit-listing/:id', component:EditListingComponent},
{path: 'add-listing', component:AddListingComponent}
]
下面是我的 EditListingComponent 代码
export class EditListingComponent implements OnInit {
id:any;
checklist:any; /*ngmodel binds the html fields to the properties in the component*/
notes:any;
constructor(private firebaseService: FirebaseService, private router:Router, private route:ActivatedRoute) { }
ngOnInit() {
// Get ID
this.id = this.route.snapshot.params['id'];
this.firebaseService.getListingDetails(this.id).subscribe(listing => {
this.checklist = listing.checklist;
this.notes = listing.notes;
console.log(listing);
});
}
onEditSubmit(){
let listing = {
checklist: this.checklist,
notes: this.notes
}
this.firebaseService.updateListing(this.id, listing).then(function() {
this.router.navigate(['/listing/'+this.id]);
});
/*this.firebaseService.updateListing(this.id, listing);
this.router.navigate(['/listings']);
}
}
我查看了与此类似的其他问题,但在对我的问题作出答复之前,我不确定这是与“this”的上下文相关的问题。
【问题讨论】:
标签: javascript angular firebase