Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 App Reloading on Route Change

I have an angular 2 app with the following routes:

@RouteConfig([
new Route({ path: '/', component: Home, name: 'Home', useAsDefault: true}),
new Route({ path: '/employees', component: ViewEmployees, name: 'ViewEmployees'}),
new Route({ path: '/employees/add', component: AddEmployee, name: 'AddEmployee'}),
])

among others. When I change routes in the following way:

<a [routerLink]="['ViewEmployees']">View Employees</a>

There are no issues. I can change routes in this way from either the home page or the AddEmployee route. The issue comes when I'm in the AddEmployee route and try to change routes in a programmatic way like this:

import {Router} from 'angular2/router';
...
constructor(private _router:Router) {}
...
navigate() {
    this._router.navigate(['ViewEmployees']);
}

it doesn't work. It sends me to the ViewEmployees view and then reloads the entire app. If I do that same programmatic route change from the Home component I don't have any issues; the app doesn't reload.

Does anyone have any ideas why it would do this in just this one case? I need it to work so that I can save the employee that's added and then go back to the employee list view.

Thanks in advance for the help!

like image 477
pjlamb12 Avatar asked Feb 29 '16 23:02

pjlamb12


People also ask

Does router navigate reload the page?

Does the page reload when we navigate to another route in react? react-router-dom allows us to navigate through different pages on our app with/without refreshing the entire component. By default, BrowserRouter in react-router-dom will not refresh the entire page.

What is the difference between navigate and navigateByUrl?

difference between the two navigateByUrl is similar to changing the location bar directly–we are providing the “whole” new URL. Whereas router. navigate creates a new URL by applying an array of passed-in commands, a patch, to the current URL.

Why do we use ActivatedRoute in angular?

ActivatedRoutelink. Provides access to information about a route associated with a component that is loaded in an outlet.

What is Route reuse strategy?

BaseRouteReuseStrategylink This base route reuse strategy only reuses routes when the matched router configs are identical. This prevents components from being destroyed and recreated when just the fragment or query parameters change (that is, the existing component is reused).


2 Answers

Do you call navigate() from within a <form> Tag?

I had the same Problem. There exist some issues describing this behavior on Angular2s GitHub but they are all closed because they belong to the old router. The page reload seems to occur when you use router.navigate() inside a function called by a submit button inside a form. This can cause the browser to append a ? at the end of the URL and reload it.

The solution is very simple: Just return false at the end of your navigate() function. This prevents the bowser to use it's default action when submitting forms. Usually angular stops such default behavior but strangely not in this case.

like image 154
A. Ziegler Avatar answered Sep 19 '22 19:09

A. Ziegler


Have you set the <base href>?

As mentioned in the Router guide

Add the following code to your index.html after the opening head tag:

<base href="/">

like image 21
pck Avatar answered Sep 22 '22 19:09

pck