当我从一个视图重定向到另一个视图时,我遇到数据绑定问题。我在“http://localhost:4200/create”视图中填写表单数据,然后重定向到“http://localhost:4200/settle/9ed5509c-ecd6-4895-96eb-a2efa04bae6d”(带令牌的网址)。重定向后没有任何东西正确绑定(根本没有绑定)但是我点击刷新(铬上的ctrl + r)然后正确地进行了绑定。
create.component.ts
onSubmit() {
this.model.token = UUID.UUID();
this.settleService
.addSettle(this.model)
.subscribe(settle => this.settles.push(settle));
this.router
.navigate([`settle`, this.model.token])
.then(() => console.log(`Redirection to settle details made!`));
this.submitted = true;
}
settle.component.ts - ngOnInit()方法
ngOnInit() {
this.getSettle();
this.currentUrl = window.location.href;
this.show = false;
}
settle.component.ts - getSettle()方法
getSettle(): void {
const token = this.route.snapshot.paramMap.get('id');
this.settleService
.getSettle(token)
.subscribe(settle => this.settle = settle, null, () => this.BindEditUrl());
}
答案 0 :(得分:2)
好的我明白了。错误很简单 - 在从后端获得响应之前进行了重定向(HTTP POST创建解决方案)。我在后端响应之后放置了重定向逻辑,现在一切正常。
工作代码:
create.component.ts - onSubmit()方法
onSubmit() {
this.model.token = UUID.UUID();
this.settleService
.addSettle(this.model)
.subscribe(settle => this.settles.push(settle), null, () => this.redirectToSettleDetails());
this.submitted = true;
}
create.component.ts - redirectToSettleDetails()方法
redirectToSettleDetails(): void {
this.router
.navigate([`settle`, this.model.token])
.then(() => console.log(`Redirection to settle details made!`));
}