角路由给出错误TypeError:无法读取未定义的属性'init'

时间:2020-01-02 12:21:14

标签: angular router

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

这是构造函数:

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;
}
}

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我注意到您提供程序数组中的一件事,您没有将IndexedDBService作为提供程序加载,因此该令牌将不能作为APP_INITIALIZER提供程序的依赖项。如果将IndexedDBService添加到您的provider数组,则应清除所有内容。

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