Angular2 router.navigate refresh page

前端 未结 5 1833
终归单人心
终归单人心 2020-11-29 09:42

This is how the routes and component look like:

routes.config

export const routes: RouterConfig = [
   { path: \'users\'         


        
5条回答
  •  情歌与酒
    2020-11-29 10:33

    You are probably calling the router.navigate function inside a click event.

    
    

    And the save function being something like

    save() {
        //Do stuff
        this._router.navigate(['/users', { id: userId } ]);
    }
    

    This works on IE11 and Edge browsers, but would reload the application in Chrome.

    This is because of a missing type in the button element, if the button is inside a

    Chrome will use 'submit' as it's default value. Causing a form submit when the button is clicked.

    It's preferred to always set a type when using the button element See here:

    So changing the HTML to

    
    

    Will make it work on all 3 browsers.

    My previous solution also works, (By returning false it would prevent the default action. a.k.a. submitting the form) but I think the above one is preferred.

    Obsolete answer but kept for posterity:

    
    

提交回复
热议问题