Angular2 refresh router-outlet

2019-08-26 07:36发布

问题:

I'm working on a CRM and I have a select box in the header which allows the user to change the current user, In addition, the main content is inside the router-outlet.

When the user select another user I want to refresh the page but only the router-outlet so the info will be for the new user

I saw that other CRMs just redirect the user to the dashboard / index page, but I want to keep the user on the same page.

回答1:

You should have a function in a service

import {Subject} from 'rxjs/Subject';

onUserChange= new Subject<void>();

userChange(): void {
    this.http.post('')
        .map()
        .subscribe((data: UserChangeResponse) => {
             this.onUserChange.next();
        })
}

And In component you can use it in ngOnInit

serviceReference.onUserChange.subscribe(() => {

     // use can call get users method or anything

});