【问题标题】:Angular Routing giving ERROR TypeError: Cannot read property 'init' of undefined角度路由给出错误类型错误:无法读取未定义的属性“init”
【发布时间】:2020-04-21 02:44:50
【问题描述】:

我在使用 router.navigate 时遇到问题。在添加路由器代码之前,我的代码运行良好,但现在它给了我这个错误“错误类型错误:无法读取未定义的属性 'init'”这是我的代码:

这是构造函数:

export class IndexedDBService {
constructor(
private _store: Store<AppState>,
private _electron: ElectronService,
private _device: Device,
private userService: UtilizadoresService,
private workTypesService: TiposTrabalhoService,
private modelTypesService: TiposModeloService,
private modelService: ModelosService,
private clientService: ClientesService,
private paymentMethodService: FormasPagamentoService,
private salesNotSent: VendasPorEnviarService,
private groupTypeWork: GruposTiposTrabalhoService,
private companies: CompanyService,
private _alertCtrl: AlertController,
private _translate: TranslateService,
private _router: Router,
) {}

这是我打电话给路由器的时候:

        this._router.navigate(['login']);

这是我调用 app.module 中的 db.init 函数时:

export function dbInitializer(db: IndexedDBService) {
return () => db.init();
}

这是我的数据库初始化函数:

public async init(): Promise<string> {
console.warn(
  isDevMode() ? '[Activation Disabled] Running in dev mode!' : ''
);

const activationCode: string = localStorage.getItem(kActivation);
let compareCode: string;
if (this._electron.isElectronApp) {
  compareCode = encrypt(this._electron.ipcRenderer.sendSync('device-info'));
} else {
  compareCode = encrypt(
    `${this._device.manufacturer}|${this._device.model}|${this._device.uuid}`
  );
}

// bypass activation when in dev mode
if (activationCode === compareCode || isDevMode()) {
  try {
    const isDbCreated = await IdbHelper.idbCon.initDb(this.getDbSchema());
    if (isDbCreated === true) {
      await (this.overwriteDB());
      return 'database created';
    } else {
      await this.overwriteDB();
      return 'database opened';
    }
  } catch (error) {
    throw error.message;
  }
} else {
  return undefined;
}
}

感谢您的帮助

【问题讨论】:

  • 你可以试试dbInitializer(private db: IndexedDBService) { return () =&gt; this.db.init();
  • 它给出了这个错误:一个参数属性只允许在构造函数实现中因为私有和'this'隐含类型'any'因为它没有类型annotation.ts因为这个
  • 你能把代码贴在你实例化 IndexedDBService 并实际调用导出函数 dbInitializer() 的地方吗?看起来您没有传入 IndexedDBService 来进行 init() 调用。
  • 这是我唯一调用 dbInitializer 提供者的地方:[ StatusBar, SplashScreen, { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }, { provide: APP_INITIALIZER, useFactory: dbInitializer, deps: [IndexedDBService], multi : true, }, 打印机, 设备, ],
  • 感谢您提供此信息。我注意到的一件事是在您的提供程序数组中,您没有将 IndexedDBService 作为提供程序加载,因此该令牌将不能作为您的 APP-INITIALIZER 提供程序的依赖项。您可以将 IndexedDBService 添加到您的 providers 数组中,看看是否可以解决问题?

标签: angular router


【解决方案1】:

我注意到的一件事是在您的 providers 数组中,您没有将 IndexedDBService 作为提供者加载,因此该令牌将不能作为您的 APP_INITIALIZER 提供者的依赖项。如果您将 IndexedDBService 添加到您的 providers 数组中,这应该可以解决问题。

  providers: [
    IndexedDBService, //added this
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    {
      provide: APP_INITIALIZER,
      useFactory: dbInitializer,
      deps: [IndexedDBService],
      multi: true
    },
    Printer,
    Device
  ],

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2020-05-09
    • 1970-01-01
    • 2019-11-16
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多