对 Angular2 世界来说有点新,但是通过指定 {reload: false} 在 Angular 1 中更简单,但无法在 Angular 2 中找到类似的解决方案,在 Angular 2 中我可以避免在更改路由中的查询参数时重新加载组件。
我有 html 模板,在其中单击按钮即可更改查询参数。
我有方法 updateRoute({type: 'app_dependency'}) 单击按钮时会调用该方法:
ngOnInit() {
console.log(`ngOnInit - is called. `);
}
public updateRoute(urlObject){
let queryParams = this.activatedRoute.snapshot.queryParams;
let updatedQuery = clone(queryParams);
this.activeTab = updatedQuery.type || 'app_dependency';
forEach(urlObject, (value, key) => {
updatedQuery[key] = value;
});
//this.location.search({type: 'app_dependency'})
this.router.navigate([], {
relativeTo: this.activatedRoute,
queryParams: queryParams
});
}
但每次调用此函数时,它都会销毁组件并调用 ngOnInit()。
是否可以只更改 url 查询(我没有更改路线,只是更改查询)而不重新加载组件?如果是,任何人都可以帮助我或建议我更好的方法。
我在这里找到了解决方案:
它将防止组件被破坏。
constructor(
private router: Router
) {
this.router.routeReuseStrategy.shouldReuseRoute = function () {
return true;
};
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)