导航到同一路径而不刷新组件?

编程入门 行业动态 更新时间:2024-10-08 03:36:51
本文介绍了导航到同一路径而不刷新组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已升级为使用已弃用路由器的新Angular 2路由器,并且某些行为已更改.

I've upgraded to use the new Angular 2 router from router-deprecated and some behavior has changed.

我遇到问题的页面是搜索页面.我们已选择使用HashLocationStrategy将所有搜索词放入URL中.路线如下所示:

The page I'm having issues with is a search page. We've chosen to put all the search terms in the URL using the HashLocationStrategy. The routes look like this:

const routes: RouterConfig = [ { path: '', component: HomeComponent }, { path: 'search/:term/:cat/:page/:sort/:size/:more', component: HomeComponent }, { path: 'search/:term/:cat/:page/:sort/:size', component: HomeComponent }, { path: 'search/:term/:cat/:page/:sort', component: HomeComponent }, { path: 'search/:term/:cat/:page', component: HomeComponent }, { path: 'search/:term/:cat', component: HomeComponent }, { path: 'search/:term', component: HomeComponent }, { path: 'details/:accountNumber', component: DetailsComponent } ];

我知道查询字符串方法可能更适合所有这些选项,但是项目需求由其他人决定...

I know a query string approach might be better fit for all these options but project requirements decided by other people...

无论如何,如果我使用this.router.navigate(['/search/Hello']);导航到localhost/#/search/Hello,则路由器可以正常工作,并且一切都很好.在该页面上,如果我尝试this.router.navigate(['/search/World']);,则浏览器地址栏中的URL将相应更新,但组件完全不变.

Anyway if I navigate to localhost/#/search/Hello using this.router.navigate(['/search/Hello']); then the router works fine and everything is great. While on that page if I try to this.router.navigate(['/search/World']); then the URL in the browser's address bar will update accordingly but the component doesn't change at all.

以前,我可以使用routerCanReuse表示我确实想重用搜索组件,并且routerOnReuse会在导航发生时重新运行搜索.我在@angular/router中没有看到与之等效的东西.如何使用新的URL参数重新加载当前路由?

Previously I could use routerCanReuse to indicate that I did want to reuse the search component and routerOnReuse would rerun the search when the navigation happened. I don't see equivalent's to these in @angular/router. How can I reload the current route with new URL parameters?

我正在为Angular 2和3.0.0-alpha.8的@ angular/router运行2.0.0-rc.3版本.

I'm running version 2.0.0-rc.3 for Angular 2 and 3.0.0-alpha.8 of @angular/router.

推荐答案

如果仅params发生了更改,则组件本身将不会再次初始化.但是您可以订阅所发送参数的更改.

If only the params has changes the component itself won't be initialize again. But you can subscribe to changes in the parameters that you send.

例如,在ngOnInit方法上,您可以执行以下操作:

For example on ngOnInit method you can do something like this:

ngOnInit() { this.sub = this.route.params.subscribe(params => { const term = params['term']; this.service.get(term).then(result => { console.log(result); }); }); }

更多推荐

导航到同一路径而不刷新组件?

本文发布于:2023-11-30 18:27:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   路径   组件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!