Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用角度2可以激活;TS很好,但我';我犯了一个错误_Angular_Angular2 Routing - Fatal编程技术网

Angular 使用角度2可以激活;TS很好,但我';我犯了一个错误

Angular 使用角度2可以激活;TS很好,但我';我犯了一个错误,angular,angular2-routing,Angular,Angular2 Routing,我是Angular 2的新手,尝试在新应用程序中保护路由。要使用的东西似乎是Can-Activate,但即使使用最简单的Can-Activate示例,似乎也会出现问题。我得到以下错误: ZoneAwareError message: "Uncaught (in promise): TypeError: Cannot set property 'stack' of undefined TypeError: Cannot set property 'stack' of undefined at No

我是Angular 2的新手,尝试在新应用程序中保护路由。要使用的东西似乎是Can-Activate,但即使使用最简单的Can-Activate示例,似乎也会出现问题。我得到以下错误:

ZoneAwareError message:
"Uncaught (in promise): TypeError: Cannot set property 'stack' of undefined
TypeError: Cannot set property 'stack' of undefined
at NoProviderError.set [as stack] (http://localhost:4200/vendor.bundle.js:6147:61)
at assignAll (http://localhost:4200/vendor.bundle.js:98482:29)
at NoProviderError.ZoneAwareError (http://localhost:4200/vendor.bundle.js:98553:16)
at NoProviderError.BaseError [as constructor] (http://localhost:4200/vendor.bundle.js:6109:16)
at NoProviderError.AbstractProviderError [as constructor] (http://localhost:4200/vendor.bundle.js:58855:16)
at new NoProviderError (http://localhost:4200/vendor.bundle.js:58904:16)
at ReflectiveInjector_._throwOrNull (http://localhost:4200/vendor.bundle.js:80617:19)
at ReflectiveInjector_._getByKeyDefault (http://localhost:4200/vendor.bundle.js:80654:25)
at ReflectiveInjector_._getByKey (http://localhost:4200/vendor.bundle.js:80604:25)
at ReflectiveInjector_.get (http://localhost:4200/vendor.bundle.js:80366:21)
at AppModuleInjector.NgModuleInjector.get (http://localhost:4200/vendor.bundle.js:59758:52)
at PreActivation.getToken (http://localhost:4200/vendor.bundle.js:24841:25)
at MapSubscriber.project (http://localhost:4200/vendor.bundle.js:24737:48)
at MapSubscriber._next (http://localhost:4200/vendor.bundle.js:11337:35)
at MapSubscriber.Subscriber.next (http://localhost:4200/vendor.bundle.js:6655:18)
我的代码如下

可以激活:

从'@angular/core'导入{Injectable};
进口{
激活,
激活的路由快照,
路由器状态快照
}来自“@angular/router”;
@可注射()
导出类CanActivateAuthGuard实现CanActivate{
canActivate(下一个:ActivatedRouteSnapshot,状态:RouterStateSnashot){
返回false;
}
}
路由模块:

从'@angular/core'导入{NgModule};
从'@angular/router'导入{RouterModule,Routes};
从“./thinks/thinklist.component”导入{ThoughtListComponent};
从“./login/login.component”导入{LoginComponent};
从“./can activate.service”导入{CanActivateAuthGuard};
常数路由:路由=[
{路径:'',路径匹配:'full',重定向到:'thoughtList'},
{path:'login',component:LoginComponent},
{path:'thoughtList',
组件:ThoughtListComponent,
canActivate:[CanActivateAuthGuard]
}
]
@NGD模块({
导入:[RouterModule.forRoot(路由)],
导出:[路由模块]
})
导出类AppRoutingModule{}
导出常量routableComponents=[
思想的组成部分,
登录组件
];
因为我使用的是TypeScript,所以我知道canActivate应该能够返回一个简单的布尔值。。。所以我不知道为什么会这样。实际的堆栈跟踪都在供应商代码中,没有对我的代码的引用

我从其他帖子中看到,zone(0.7.3)的错误版本可能会导致错误,但我已经尝试了应该可以工作的版本(0.7.2和0.7.4)。应用程序的其余部分是基于angular cli构建的


我还注意到,当我只注释掉路由的canActivate属性时,我的代码工作得很好。。。因此,canActivate的某些特定功能正在中断。

CanActivateAuthGuard
添加到模块的提供程序中应该可以修复此错误。

是否已将CanActivateAuthGuard添加到模块的提供程序中?路由中似乎缺少提供程序声明