TypingPanda TypingPanda - 4 months ago 46
AngularJS Question

Angular 2 Routing: Refresh page with parameter get "Fail to load resource" error

I am learning Angular 2 from the official tutorial. I have just completed the latest routing tutorial. What keeps bothering me is that I am able to click through to the path till the detail level which is something like 'http://domainname:portnumber/crisis-center/11' where 'crisis-center' is a subpath and '11' is the id passed in a parameter.

what I found is if I navigate from the root path which is 'http://domainname:portnumber', then I could go through to the detail page without any trouble. But if I open a new window and directly visit the detail view page then it gives me error says
enter image description here

which I believe is a failure of loading the loading 'js' files.

I have already had the base ulr tag

in the index.html page

<html>

<head>



<title>Tour of Heroes</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">


<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>

<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>

<base href="/">


</head>

<body>
<my-app>Loading...</my-app>
</body>



</html>


and here is my path settings

import {RouterConfig} from '@angular/router';
import {CrisisDetailComponent} from './crisis-detail.component';
import {CrisisListComponent} from './crisis-list.component';
import {CrisisCenterComponent} from './crisis-center.component';
import {CrisisAdminComponent} from './crisis-admin.component';
import {AuthGuard} from '../auth.guard';

export const CrisisCenterRoutes: RouterConfig = [
{
path: '',
redirectTo: '/crisis-center',
terminal: true
},

{
path: 'crisis-center',
component: CrisisCenterComponent,
children: [
{ path: 'admin', component: CrisisAdminComponent, canActivate: [AuthGuard] },
{ path: ':id', component: CrisisDetailComponent },
{ path: '', component: CrisisListComponent }



]
}
]


Has anyone had similar issue and fixed it?

Thanks

Answer

I believe you are missing one tiny line in your main index.html:

<base href="/">

as mentioned here https://angular.io/docs/ts/latest/guide/router.html#!#base-href