如何通过打字稿在angular2更改路由器?(How to change router by typ

2019-10-24 02:25发布

比如,我用这样的路由器链接:

<li><a [router-link]="['/start']">Start</a></li>

但是,我怎么能以打字稿更改路由器/启动?

Answer 1:

小更新相关的哈希定位策略。

在最新版本的angular2的bind方法已经过时,所以你可以通过改变定位策略provide方法。

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


Answer 2:

我相信你问如何配置角2的路线。

  • 1)进口和加载路由器
  • 2)使用@RouteConfig设置你的组件上的路线

  • 可选:添加一个hashbang(#)到您的网址

下面是一个例子:

import {Component, View, bind, bootstrap} from 'angular2/angular2';
import {routerInjectables, routerDirectives, Router, RouteConfig} from 'angular2/router';
import {LocationStrategy, Location, HashLocationStrategy } from 'angular2/router'; // options2: HTML5LocationStategy

// Components
import {Home} from 'home';
import {SomewhereElse} from 'somePlace';

@Component({
  selector: 'app-name'
})
@View({
  template: '<router-outlet></router-outlet>',
  directives: [routerDirectives]
})
@RouteConfig([
  {path: '/start', as:  component: Home},
  {path: '/place/:placeId', component: SomewhereElse}
])
class AppName {}

bootstrap(AppName, [
  routerInjectables,
  bind(LocationStrategy).toClass(HashLocationStrategy) // for hashbang routes (/#/)
  // alternative: use HTML5LocationStrategy
]);


文章来源: How to change router by typescript in angular2?