Angular2-Dart PathLocationStrategy

时间:2017-02-10 01:19:19

标签: dart angular2-dart

使用HashLocationStrategy刷新angular2 dart页面工作正常,因为它打开完全相同的视图。

使用PathLocationStrategy刷新页面 - 配置为服务于index.html的tomcat服务器 - 适用于没有参数的url,但不适用于带参数的url。

localhost:8090 / menu1 //刷新工作
localhost:8090 / menu2 / paramVal //不刷新

tomcat web.xml已

  <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 个答案:

答案 0 :(得分:1)

我没有使用404来投放index.html,而是想要设置servlet-mapping <url-pattern>*</url-pattern>

另见Tomcat servlet, redirect all urls to a single webpage