Cor*_*urn 44 angular2-routing angular
我已升级为使用路由器弃用的新Angular 2路由器,并且某些行为已更改.
我遇到问题的页面是搜索页面.我们选择使用HashLocationStrategy.将所有搜索字词放在网址中.路线看起来像这样:
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 }
];
Run Code Online (Sandbox Code Playgroud)
我知道查询字符串方法可能更适合所有这些选项,但项目要求由其他人决定......
无论如何,如果我导航到localhost/#/search/Hello使用,this.router.navigate(['/search/Hello']);那么路由器工作正常,一切都很好.在该页面上,如果我尝试this.router.navigate(['/search/World']);然后浏览器地址栏中的URL将相应更新,但组件根本不会更改.
以前我可以routerCanReuse用来表明我确实想重用搜索组件,并routerOnReuse在导航发生时重新运行搜索.我没有看到与这些相同的东西@angular/router.如何使用新的URL参数重新加载当前路由?
我正在运行Angular 2的2.0.0-rc.3版和@ angular/router的3.0.0-alpha.8版.
Dor*_*man 50
如果只有参数更改,则组件本身不会再次初始化.但您可以订阅您发送的参数的更改.
例如,在ngOnInit方法中,您可以执行以下操作:
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
const term = params['term'];
this.service.get(term).then(result => { console.log(result); });
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28734 次 |
| 最近记录: |