Angular2-Dart PathLocationStrategy

284 views Asked by At

Refreshing angular2 dart page using HashLocationStrategy works fine as it opens the exact same view.

Refreshing the page using PathLocationStrategy - with tomcat server configured to serve index.html - works for the url without parameter but does not work for the url with parameter.

localhost:8090/menu1 // refresh works
localhost:8090/menu2/paramVal // does not refresh

tomcat web.xml has

  <error-page>
    <error-code>404</error-code>
    <location>/index.html</location>
  </error-page>

main.dart

main() {  
      bootstrap(AppComponent, [  
      ROUTER_PROVIDERS,  
      provide(APP_BASE_HREF, useValue: '/')]);  
}  

app_component.dart

import 'package:angular2/core.dart';
import 'package:angular2/router.dart';
import 'package:angular2/angular2.dart';
import 'package:mboxes/menu1.dart';
import 'package:mboxes/menu2.dart';

@Component(
    selector: 'my-app',
    templateUrl: 'app_component.html',
    directives: const [ROUTER_DIRECTIVES],
    providers: const[ROUTER_PROVIDERS, ])

@RouteConfig(const [
  const Route(
      path: '/menu1',
      name: 'Menu1',
      component: Menu1Component,
      useAsDefault: true),
  const Route(
      path: '/menu2/:param', name: 'Menu2', component: Menu2Component)
])
class AppComponent {}

app_component.html

<div class="container">
    <nav>
        <ul>
            <li>
                <a [routerLink]="['Menu1']">Menu1</a>
            </li>
            <li> <a [routerLink]="['Menu2', {'param':'paramVal'}]">Menu2</a> </li>
        </ul>
    </nav>

    <div style="padding-left: 200px; padding-top: 200px; padding-bottom: 50px">
    <router-outlet></router-outlet>
    </div>
</div>

menu1.dart

import 'package:angular2/core.dart';
import 'package:angular2/router.dart';
@Component(
    selector: 'menu1',
    template: ''' menu 1 was clicked '''
)
class Menu1Component {}

menu2.dart

import 'package:angular2/core.dart';
import 'package:angular2/router.dart';
@Component(
    selector: 'menu2',
    template: ''' menu 2 was clicked'''
)
class Menu2Component implements OnInit {
  final RouteParams _routeParams;
  Menu2Component(this._routeParams);
  ngOnInit()  {
    var val  = _routeParams.get('param');
    print  ("passed param is " +  val);
  }
}
1

There are 1 answers

0
Nate Bosch On

Instead of using 404 to serve index.html I think you want to set up a servlet-mapping with something like <url-pattern>*</url-pattern>

See also Tomcat servlet, redirect all urls to a single webpage