首页 > 解决方案 > 使用 Capacitor CallNumber 获取无效的提供程序错误

问题描述

我正在实施 CallNumber。如果我遵循ionic 文档,则没有提到在提供程序中包含 CallNumber,因此会引发错误:

错误:未捕获(承诺中):NullInjectorError:R3InjectorError(HomePageModule)[CallNumber -> CallNumber -> CallNumber -> CallNumber]:NullInjectorError:没有提供 CallNumber!

所以我在 app.module.ts 中添加 CallNumber 作为提供者

import { CallNumber } from '@ionic-native/call-number';


@NgModule({
  declarations: [AppComponent, StudentModalPage],
  imports: [
    BrowserModule.withServerTransition({ appId: 'serverApp' }),
    BrowserTransferStateModule,
    IonicStorageModule.forRoot(),
    IonicModule.forRoot(),
    FormsModule,
    AppRoutingModule,
    HttpClientModule,
    ComponentsModule,
    ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })
  ],
  providers: [
   CallNumber,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
   
    {
      provide: APP_INITIALIZER,
      useFactory: (platformId: object, response: any) => {
        return () => {
          // In the server.ts we added a custom response header with information about the device requesting the app
          if (isPlatformServer(platformId)) {
            if (response && response !== null) {
              // Get custom header from the response sent from the server.ts
              const mobileDeviceHeader = response.get('mobile-device');

              // Set Ionic config mode?
            }
          }
        };
      },
      deps: [PLATFORM_ID, [new Optional(), RESPONSE]],
      multi: true
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {

但现在我得到这个错误:

错误:NgModule 'AppModule' 的提供程序无效 - 只允许提供 Provider 和 Type 的实例,得到:[?[object Object]?, ..., ...]

我已经搜索了分配,找不到解决方案,也没有隐瞒错误,任何输入表示赞赏。

标签: ionic-frameworkphone-call

解决方案


我遇到类似问题的解决方案,并从添加提供程序后创建的自动导入更改了我的导入:

 { CallNumber } from '@ionic-native/call-number';

至:

{ CallNumber } from '@ionic-native/call-number/ngx';

推荐阅读