简体   繁体   English

在Angular 2中重置或重新加载同一页面

[英]reset or reload the same page in Angular 2

I am trying to reload the same url in Angular 2 by using router.navigate but it is not working. 我试图通过使用router.navigate重新加载Angular 2中的相同URL,但它无法正常工作。

Url: http://localhost:3000/page1 网址: http:// localhost:3000 / page1

Scenario: I am on http://localhost:3000/landing and on click of button, will pass routing parameter which should reload the page. 场景:我在http:// localhost:3000 / landing上,点击按钮,会传递应该重新加载页面的路由参数。

Example: 例:

Suppose user is in page1(Edit form) and url reads as localhost:3000/page1 and there will be a button Create new, on click of button passing a routing paramter using 假设用户在page1(编辑表单)并且url读取为localhost:3000/page1并且将有一个按钮创建新,在点击按钮时使用路由参数

let navigationExtras: NavigationExtras = { queryParams: { "refresh": "Y" } }; 
this.router.navigate(['page1'], navigationExtras);

ngOnInit is only called when the component is created. 仅在创建组件时调用ngOnInit。 When you navigate to the same page (with different url parameters) the ngOnInit method is not recalled. 当您导航到同一页面(具有不同的url参数)时,不会调用ngOnInit方法。 You should use the ActivatedRoute Params Observable 您应该使用ActivatedRoute Params Observable

import { ActivatedRoute } from '@angular/router';

export class MyComponent implements OnInit {
  constructor(private _activatedRoute: ActivatedRoute){}

  ngOnInit(){
    this._activatedRoute.params.subscribe(
      params => {
        // this is called everytime the url changes
      }
   }
}

你只需要调用window.location.reload();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM