为什么提供程序在组件中不起作用?

时间:2020-10-23 14:24:29

标签: angular

我使用工厂提供PAGINATION_INTERAGENCY

export const PAGINATION_INTERAGENCY = new InjectionToken<any>('PAGINATION_INTERAGENCY');

 providers: [
        {
            provide: PAGINATION_INTERAGENCY,
            useFactory: () =>
                new PaginationService({
                    id: 'serverPagination',
                    offset: 0,
                    limit: 20,
                    sortByDefault: '+docid',
                    pageSizeOptions: [10, 20, 100],
                    currentPage: 1,
                    itemsPerPage: 20,
                }),
        },
        InteragencyCooperationService,
        InteragencyCooperationRepository,
    ],

然后在内部构造函数中使用提供程序:

constructor(
    @Inject(PAGINATION_INTERAGENCY) private paginationService: PaginationService) {}

第一次加载模块时出现此错误:

ERROR NullInjectorError: R3InjectorError(InteragencyModule)[PaginationService -> PaginationService -> PaginationService -> PaginationService]: 
  NullInjectorError: No provider for PaginationService!

如何解决?

1 个答案:

答案 0 :(得分:1)

您的注射是错误的。 在这里看看:factory-providers-usefactory | angular.io

大概这样会起作用:

    {
        provide: PAGINATION_INTERAGENCY,
        useFactory: PaginationService,
        deps: [/*your dependencies goes here*/]
    }