In Angular2, I am facing this issue. When you refresh the page. The URL remains same but it doesn't load appropriate view in RouterOutlet
.
Everything works fine except refreshing page issue.
app.ts
import {Component,bind} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {FORM_DIRECTIVES} from 'angular2/form';
import{HomeCmp} from 'Angular/src/Home/home.ts';
import {ContactUsCmp} from 'Angular/src/ContactUs/contactus.ts';
import {Router,ROUTER_PROVIDERS,RouteConfig, ROUTER_DIRECTIVES,APP_BASE_HREF,LocationStrategy,RouteParams,ROUTER_BINDINGS} from 'angular2/router';
@Component({
selector: 'micropuppy-app',
templateUrl: "ANGULAR/Templates/home.html",
directives:[HomeCmp,ROUTER_DIRECTIVES,ContactUsCmp],
template: ` <nav>
<ul class="nav navbar-nav">
**<li><a [routerLink]="['Home']">One</a><hr/></li>
<li><a [routerLink]="['ContactUs']">Contact Us</a></li>**
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Technologies <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Angular</a></li>
<li><a href="#">.NET</a></li>
</ul>
</li>
</ul>
</nav>
<router-outlet></router-outlet>`
})
@RouteConfig([
{path:'/Home', name: 'Home', component: HomeCmp}
{path:'/ContactUs', name: 'ContactUs', component: ContactUsCmp}
])
export class MicropuppyApp {
constructor(){
console.log("Micropuppy app started");
}
}
bootstrap(MicropuppyApp, [ROUTER_PROVIDERS,bind(APP_BASE_HREF).toValue(location.pathname)]);
I managed to fix this issue by putting this code on the top of head in the
index.html
under<title>
putEven I was stuck in the same situation where on refresh Angular2 app was failing to load.
The reason behind this issue is Angular2 router uses history API for routing due to which on the refresh of route App unable to find index.html and end up showing 404 or can not GET requested URL.
you can work around in two ways:
Hashbang technique by using HashLocationStrategy but URL looks bad due to present of hash.
Here how I worked it out:
I use Node.js for serving the request.
Assuming you have Node and Npm installed.
package.json for installing required modules.
create server.js in your project directory.
Use the following command to start your app server.
Now every request will go through your App server which will make sure that on every refresh/reload index.html get served which will boost angular App.
With the default strategy (HTML5 history API) of routing, you need a server configuration to redirect all your paths to your HTML entry point file. With the hashbang approach it's not necessary... If you want to switch to this approach, simply use the following code:
You could have a look at these questions about this issue:
Hope it helps you, Thierry
This is not the right answer but On-refresh you can redirect all dead calls to Homepage by sacrificing 404 page it's a temporary hack just replay following on 404.html file
If you don' want # means, go with 'PathLocationStrategy'. It is somewhat similar to HashLocationStratedy. Hope, this link will help you Angular2 routing.