Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.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
Javascript Angular2-将服务注入服务时出现无效的提供程序错误_Javascript_Angular_Authentication - Fatal编程技术网

Javascript Angular2-将服务注入服务时出现无效的提供程序错误

Javascript Angular2-将服务注入服务时出现无效的提供程序错误,javascript,angular,authentication,Javascript,Angular,Authentication,我目前有如下模块设置(EXPERPT) 我已将我的AuthService(负责处理用户身份验证并提供确定当前用户是否经过身份验证的方法)定义为我的AuthModule中的提供者 // auth.module.ts - uses https://github.com/auth0/angular2-jwt export function authHttpServiceFactory(http: Http, options: RequestOptions) { return new AuthHt

我目前有如下模块设置(EXPERPT)

我已将我的
AuthService
(负责处理用户身份验证并提供确定当前用户是否经过身份验证的方法)定义为我的
AuthModule
中的提供者

// auth.module.ts - uses https://github.com/auth0/angular2-jwt

export function authHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig({
    tokenName: jwtLocalStorageKey
  }), http, options);
}

export let authHttpServiceProvider = {
  provide: AuthHttp,
  useFactory: authHttpServiceFactory,
  deps: [Http, RequestOptions]
};

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule
  ],
  exports: [AuthComponent],
  declarations: [AuthComponent, LoginComponent, RegisterComponent],
  providers: [
    AuthService,
    authHttpServiceProvider
  ]
})
export class AuthModule { }
我可以在它的同级
LoginFormComponent
中毫无问题地使用此服务。当我试图在
RoutingModule
中的
AuthRouteGuard
类中使用
AuthService
时,我得到以下错误:

Error: Invalid provider for the NgModule 'AuthModule' - only instances of Provider and Type are allowed, got: [?undefined?, ...]
我将
AuthModule
导入到
RoutingModule
中。当
AuthService
被定义为
AuthRouteGuard
的依赖项时,就会发生上述错误

export class AuthRouteGuard implements CanActivate {
  constructor(
    private router: Router,
    private authService: AuthService // Removing this injection removes the error
  ) {}

  canActivate() {
    // @todo: if not authenticated
    this.router.navigate(['/login']);

    return true;
  }
}
我在这里遗漏了什么,为什么在构造函数中注入服务会导致无效的提供程序错误,而在移除注入时不会发生这种错误

编辑-如果将
authHttpServiceProvider
提供程序一起删除,则会发生相同的错误,因此
AuthModule
模块如下所示:

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule
  ],
  exports: [AuthComponent],
  declarations: [AuthComponent, LoginComponent, RegisterComponent],
  providers: [
    AuthService
  ]
})
export class AuthModule { }

authHttpServiceProvider
添加到模块的导入中。它已导出到全局,不可用于模块。因此,您无法提供服务,因为模块的提供程序未知

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule
  ],
  exports: [AuthComponent],
  declarations: [AuthComponent, LoginComponent, RegisterComponent],
  providers: [
    AuthService
  ]
})
export class AuthModule { 

实际问题在
AuthService
本身内部

AuthModule
定义了一个常量

export const jwtKey = 'jwt';
正在导入到
AuthService
并使用的

import { jwtKey } from '../auth.module';

出于某种原因,如果我删除此导入,一切正常。

导入到
AuthModule
?这不可能是真的,因为如果我删除
RoutingModule
中的用法,它在
AuthModule
中的
LoginFormComponent
中工作正常。
RoutingModule
如何知道提供者?如果删除模块,则删除了错误的提供程序。这是一个解决方案。如果我去掉与身份验证提供程序相关的所有内容(因此,
AuthModule
中唯一的提供程序是
AuthService
发生了相同的错误,因此与此无关。如果你做了一些不起作用的事情,那么它当然不起作用。你做错了。这甚至没有任何意义。我是说,如果我完全删除了提供程序,你说是导致它的原因发生ame错误,因此该错误无法与该提供程序关联。
AuthModule
是延迟加载模块吗?不是@BougarfaouiElhoucine,只是一个普通模块
import { jwtKey } from '../auth.module';