how to refresh page in angular 2

后端 未结 6 1395
情话喂你
情话喂你 2021-02-02 08:19

I have created one router link as below. This router link loads ProductsStartComponent and then this component loads several other components using ngif and not via

相关标签:
6条回答
  • 2021-02-02 08:46

    Without a bit more code ... its hard to say what's going on.

    But if your code looks something like this:

    <li routerLinkActive="active">
      <a [routerLink]="/categories"><p>Products Categories</p></a>
    </li>
    ...
    <router-outlet></router-outlet>
    <myComponentA></myComponentA>
    <myComponentB></myComponentB>
    

    Then clicking on the router link will route to the categories route and display its template in the router outlet.

    Hiding and showing the child components don't affect what is displayed in the router outlet.

    So if you click the link again, the categories route is already displayed in the router outlet and it won't display/re-initialize again.

    If you could be a bit more specific about what you are trying to do, we could provide more specific suggestions for you. :-)

    0 讨论(0)
  • 2021-02-02 08:48

    window.location.reload() or just location.reload()

    0 讨论(0)
  • 2021-02-02 08:58

    If you want to reload the page , you can easily go to your component then do :

    location.reload();
    
    0 讨论(0)
  • 2021-02-02 08:58

    Updated

    How to implement page refresh in Angular 2+ note this is done within your component:

    location.reload();
    
    0 讨论(0)
  • 2021-02-02 09:00

    The simplest possible solution I found was:

    In your markup:

    <a [href]="location.path()">Reload</a>
    

    and in your component typescript file:

    constructor(
            private location: Location
      ) { }
    
    0 讨论(0)
  • 2021-02-02 09:04

    Just in case someone else encounters this problem. You need to call

    window.location.reload()
    

    And you cannot call this from a expression. If you want to call this from a click event you need to put this on a function:

    (click)="realodPage()"
    

    And simply define the function:

    reloadPage() {
       window.location.reload();
    }
    

    If you are changing the route, it might not work because the click event seems to happen before the route changes. A very dirty solution is just to add a small delay

    reloadPage() {
        setTimeout(()=>{
          window.location.reload();
        }, 100);
    }
    
    0 讨论(0)
提交回复
热议问题