是否可以通过同一个routerLink刷新组件映射?让我们说这种情况:
我有一些路由:
const appRoutes: Routes = [
//other Routes...
{path: 'some/:formCode', component: SomeComponent},
//other Routes...
]
用户输入链接/some/dwa21e
。应用程序根据dwa21e
代码动态加载表单。显示动态表单(代码唯一)。
用户可以填写表格。完成表单时,其他操作在SomeComponent中执行,但在表单外部执行。
用户尝试提交表单。有两种选择:
验证错误:显示错误消息。 SomeComponent保持与提交前相同的状态。
验证正确: SomeComponent返回其状态,如 2。
在正确提交表单后,我遇到了重定向到/some/dwa21e
的问题。如果我尝试重定向到当前URL,则没有任何反应。 是否可以将组件刷新到初始状态?也许,有没有其他方法可以做到这一点?
我想强调,清算形式还不够,因为来自 3的行动。点
答案 0 :(得分:0)
你应该用组件制作一个空路线。
此组件将重定向到/some/dwa21e
上的ngOnInit()
;
只需将dwa21e
参数值发送到该空组件,即可以使用值重定向到/some
。
据我所知,除非你刷新整个页面,否则无法刷新组件。
答案 1 :(得分:0)
当想要刷新组件时,可以选择订阅路径参数。这样您就可以再次触发ngOnInit()
中的代码。
import { ActivatedRoute } from '@angular/router';
...
currentId: string;
constructor(private _route: ActivatedRoute) {
this.currentId = _route.snapshot.params['id'];
}
ngOnInit() {
this._route.params.subscribe(params => {
this.currentId = params['id'];
this.functionToLoad();
}
}