0

Here is my project directory structure:

--root
   --app
      --app.ts
      --boot.ts
   --index.html 
   --node_modules

Here is my code:

boot.ts

    bootstrap(AppComponent, [
        ROUTER_PROVIDERS,
        provide(LocationStrategy, {useClass: HashLocationStrategy})
    ]);

app.ts

@RouteConfig([
    {path: '/', name: 'root', redirectTo: ['/pageA']},
    {path: '/page-a', name: 'pageA', component: PageA},
    {path: '/page-b', name: 'pageB', component: PageB}
])

html file:

<head>
    <base href="/">
    <script src="node_modules/angular2/bundles/router.dev.js"></script>
    ...
</head>

It works, but when I look at the console, it tell me "http://localhost:63342/#/page-a Failed to load resource: the server responded with a status of 404 (Not Found)"

In addition, when I refresh the page, it shows the "404 not found" page.

Ng2-Fun
  • 3,307
  • 9
  • 27
  • 47

2 Answers2

1

I would use useAsDefault instead of redirectTo:

@RouteConfig([
    {path: '/page-a', name: 'pageA', component: PageA, useAsDefault: true},
    {path: '/page-b', name: 'pageB', component: PageB}
])

In the case of the HashLocationStrategy strategy, using isn't necessary...

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
0

Finally solved the problem by using two ways, the first way is highly recommended.

1.see this post for details: Angular 2 router no base href set

<base href="/root/">


2.without setting <base href...>, I can't figure out the reason yet.


3.from documentation:

<script>document.write('<base href="' + document.location + '" />');</script>

Get hint from the official documentation and examples: https://angular.io/docs/ts/latest/guide/router.html
http://plnkr.co/edit/?p=preview

Notes from documentation:

Live example note

We have to be get tricky when we run the live example because the host service sets the application base address dynamically. That's why we replace the <base href...> with a script that writes a <base> tag on the fly to match.
<script>document.write('<base href="' + document.location + '" />');</script>
We should only need this trick for the live example, not production code.
Community
  • 1
  • 1
Ng2-Fun
  • 3,307
  • 9
  • 27
  • 47