Angular Routing giving ERROR TypeError: Cannot read property 'init' of undefined
Angular Routing giving ERROR TypeError: Cannot read property 'init' of undefined
我在使用 router.navigate.Before 时遇到问题 我添加了路由器代码 我的代码运行良好但现在它给我这个错误 "ERROR TypeError: Cannot read property 'init' of undefined" 这是我的代码:
这是构造函数:
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']);
这是我调用 db.init 函数的时候,该函数位于 app.module:
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;
}
}
感谢您的帮助
我注意到的一件事是在您的提供程序数组中,您没有将 IndexedDBService 作为提供程序加载,因此该令牌将无法作为您的 APP_INITIALIZER 提供程序的依赖项使用。如果您将 IndexedDBService 添加到您的供应商数组中,那应该可以解决问题。
providers: [
IndexedDBService, //added this
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
{
provide: APP_INITIALIZER,
useFactory: dbInitializer,
deps: [IndexedDBService],
multi: true
},
Printer,
Device
],
我在使用 router.navigate.Before 时遇到问题 我添加了路由器代码 我的代码运行良好但现在它给我这个错误 "ERROR TypeError: Cannot read property 'init' of undefined" 这是我的代码:
这是构造函数:
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']);
这是我调用 db.init 函数的时候,该函数位于 app.module:
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;
}
}
感谢您的帮助
我注意到的一件事是在您的提供程序数组中,您没有将 IndexedDBService 作为提供程序加载,因此该令牌将无法作为您的 APP_INITIALIZER 提供程序的依赖项使用。如果您将 IndexedDBService 添加到您的供应商数组中,那应该可以解决问题。
providers: [
IndexedDBService, //added this
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
{
provide: APP_INITIALIZER,
useFactory: dbInitializer,
deps: [IndexedDBService],
multi: true
},
Printer,
Device
],