Angular2路由器可以激活虚假

时间:2016-07-26 15:50:54

标签: angular angular2-routing

所以我的代码看起来像这样:

export const routes: RouterConfig = [
  { 'path': '', redirectTo: '/jobs', terminal: true, canActivate: [tokenNotExpired()]  },
  { path: 'account', component: AccountComponent, canActivate: [!tokenNotExpired()] },
  { path: 'login', component: LoginComponent },
  ...WihRoutes
];

每当我尝试导航到" account"时,路由都会失败,但由于某种原因,它也会出现以下控制台错误:

  

browser_adapter.ts:82 EXCEPTION:错误:未捕获(在承诺中):否   提供者为false!BrowserDomAdapter.logError @   browser_adapter.ts:82BrowserDomAdapter.logGroup @   browser_adapter.ts:93ExceptionHandler.call @   exception_handler.ts:58(匿名函数)@   application_ref.ts:374schedulerFn @   async.ts:148SafeSubscriber .__ tryOrUnsub @   Subscriber.ts:240SafeSubscriber.next @   Subscriber.ts:192Subscriber._next @ Subscriber.ts:133Subscriber.next @   Subscriber.ts:93Subject._finalNext @ Subject.ts:154Subject._next @   Subject.ts:144Subject.next @ Subject.ts:90EventEmitter.emit @   async.ts:133onError @ ng_zone.ts:142onHandleError @   ng_zone_impl.ts:95ZoneDelegate.handleError @   zone.js:327Zone.runGuarded @ zone.js:233_loop_1 @   zone.js:487drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @   zone.js:426 browser_adapter.ts:82   STACKTRACE:BrowserDomAdapter.logError @   browser_adapter.ts:82ExceptionHandler.call @   exception_handler.ts:61(匿名函数)@   application_ref.ts:374schedulerFn @   async.ts:148SafeSubscriber .__ tryOrUnsub @   Subscriber.ts:240SafeSubscriber.next @   Subscriber.ts:192Subscriber._next @ Subscriber.ts:133Subscriber.next @   Subscriber.ts:93Subject._finalNext @ Subject.ts:154Subject._next @   Subject.ts:144Subject.next @ Subject.ts:90EventEmitter.emit @   async.ts:133onError @ ng_zone.ts:142onHandleError @   ng_zone_impl.ts:95ZoneDelegate.handleError @   zone.js:327Zone.runGuarded @ zone.js:233_loop_1 @   zone.js:487drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @   zone.js:426 browser_adapter.ts:82错误:未捕获(在承诺中):否   提供者是假的!       at resolvePromise(zone.js:538)       在zone.js:515       在ZoneDelegate.invoke(zone.js:323)       at Object.onInvoke(ng_zone_impl.ts:72)       在ZoneDelegate.invoke(zone.js:322)       在Zone.run(zone.js:216)       在zone.js:571       在ZoneDelegate.invokeTask(zone.js:356)       at Object.onInvokeTask(ng_zone_impl.ts:61)       在ZoneDelegate.invokeTask(zone.js:355)

也是错误的图片,因为格式化很糟糕。 Stacktrace

我不清楚为什么会这样 - 其他人?

1 个答案:

答案 0 :(得分:0)

传递给canActivate: [!tokenNotExpired()]的值必须是提供者。 !tokenNotExpired()不是有效的提供商。

canActivate: ['tokenExpired']
canActivate: ['!tokenExpired']

并在bootstrap或APP_ROUTER_PROVIDERS

中提供
bootstrap(AppComponent, [
  {provide: 'tokenExpired', useValue: (a: any, b: any) => tokenExpired()}
  {provide: '!tokenExpired', useValue: (a: any, b: any) => !tokenExpired()}
]);