有什么办法可以防止 RouterEvent 触发吗?
Is there any way to prevent RouterEvent firing?
我有一个应用程序,我需要在 URL 字符串中保留过滤器表单的状态。
可以看到实现here
在更改表单值时,我想将其设置为 URL 状态,但 this.router.navigation([], { queryParams })
发出了那里不需要的事件。
data$ = merge(
this.router.events.pipe(
filter((e: NavigationEnd) => e instanceof NavigationEnd),
map(({ url }: NavigationEnd) => {
const { queryParams } = this.router.parseUrl(url);
if (queryParams['completed']) {
queryParams['completed'] = queryParams['completed'] === 'true';
}
this.form.patchValue(queryParams, { emitEvent: false });
return queryParams;
}),
),
this.form.valueChanges.pipe(
debounceTime(200),
tap((queryParams: Task) => {
this.router.navigate([], { queryParams })
})
)
).pipe(
switchMap((query?: Task) => this.getData(query))
);
是否有可能以某种方式阻止导航事件的发出?
需要与 this.form.patchValue(queryParams, { emitEvent: false });
类似的东西
location.go(url)
就是你所需要的。
您始终可以在 router.createUrlTree()
方法
中生成适当的 url
const url = this.router
.createUrlTree(
[{foo: 'bar'}],
{relativeTo: this.activatedRoute}
)
.toString();
this.location.go(url);
我有一个应用程序,我需要在 URL 字符串中保留过滤器表单的状态。
可以看到实现here
在更改表单值时,我想将其设置为 URL 状态,但 this.router.navigation([], { queryParams })
发出了那里不需要的事件。
data$ = merge(
this.router.events.pipe(
filter((e: NavigationEnd) => e instanceof NavigationEnd),
map(({ url }: NavigationEnd) => {
const { queryParams } = this.router.parseUrl(url);
if (queryParams['completed']) {
queryParams['completed'] = queryParams['completed'] === 'true';
}
this.form.patchValue(queryParams, { emitEvent: false });
return queryParams;
}),
),
this.form.valueChanges.pipe(
debounceTime(200),
tap((queryParams: Task) => {
this.router.navigate([], { queryParams })
})
)
).pipe(
switchMap((query?: Task) => this.getData(query))
);
是否有可能以某种方式阻止导航事件的发出?
需要与 this.form.patchValue(queryParams, { emitEvent: false });
类似的东西
location.go(url)
就是你所需要的。
您始终可以在 router.createUrlTree()
方法
const url = this.router
.createUrlTree(
[{foo: 'bar'}],
{relativeTo: this.activatedRoute}
)
.toString();
this.location.go(url);