Angular 无法通过useClass在提供的类中使用@Inject()
我正在尝试在我创建的子模块中用我自己的替换默认的Angular ErrorHandler。但是,它会抛出一个NullInjectorError 我已经把问题缩小到围绕@InjectSENTRY_选项。因为当我从SentryErrorHandler中删除构造函数时,它就开始工作了 问题是。我想注入配置,但无法找出我做错了什么,因为我似乎做了与配置相同的事情 应用模块 岗哨模块 必须导出InjectionToken:-/ 它有点不直观,因为它只在该文件中使用Angular 无法通过useClass在提供的类中使用@Inject(),angular,typescript,Angular,Typescript,我正在尝试在我创建的子模块中用我自己的替换默认的Angular ErrorHandler。但是,它会抛出一个NullInjectorError 我已经把问题缩小到围绕@InjectSENTRY_选项。因为当我从SentryErrorHandler中删除构造函数时,它就开始工作了 问题是。我想注入配置,但无法找出我做错了什么,因为我似乎做了与配置相同的事情 应用模块 岗哨模块 必须导出InjectionToken:-/ 它有点不直观,因为它只在该文件中使用 export const SENTRY_
export const SENTRY_OPTIONS = new InjectionToken<BrowserOptions>('SentryOptions');
^^^^^^
如果您在SentryModule的@NgModule decorator中的providers SentryErrorHandler下放置,会有帮助吗?它没有帮助。
import {
ErrorHandler,
Inject,
Injectable,
InjectionToken,
ModuleWithProviders,
NgModule,
} from '@angular/core';
import { BrowserOptions, captureException, init } from '@sentry/browser';
const SENTRY_OPTIONS = new InjectionToken<BrowserOptions>('SentryOptions');
@Injectable()
export class SentryErrorHandler implements ErrorHandler {
public constructor(@Inject(SENTRY_OPTIONS) config: BrowserOptions) {
init(config);
}
public handleError(error: Error | any) {
captureException(error.originalError || error);
throw error;
}
}
@NgModule()
export class SentryModule {
public static forRoot(sentryOptions?: BrowserOptions): ModuleWithProviders {
return {
ngModule: SentryModule,
providers: [
{ provide: SENTRY_OPTIONS, useValue: sentryOptions },
{ provide: ErrorHandler, useClass: SentryErrorHandler },
],
};
}
}
Uncaught NullInjectorError: StaticInjectorError(AppModule)[ErrorHandler -> SentryErrorHandler]:
StaticInjectorError(Platform: core)[ErrorHandler -> SentryErrorHandler]:
NullInjectorError: No provider for SentryErrorHandler!
at NullInjector.get (http://localhost:4200/vendor.js:36537:27)
at resolveToken (http://localhost:4200/vendor.js:36864:24)
at tryResolveToken (http://localhost:4200/vendor.js:36790:16)
at StaticInjector.get (http://localhost:4200/vendor.js:36653:20)
at resolveToken (http://localhost:4200/vendor.js:36864:24)
at tryResolveToken (http://localhost:4200/vendor.js:36790:16)
at StaticInjector.get (http://localhost:4200/vendor.js:36653:20)
at resolveNgModuleDep (http://localhost:4200/vendor.js:58301:29)
at _createClass (http://localhost:4200/vendor.js:58369:29)
at _createProviderInstance (http://localhost:4200/vendor.js:58334:26)
export const SENTRY_OPTIONS = new InjectionToken<BrowserOptions>('SentryOptions');
^^^^^^