i am making a API call after the QR code scan,in the qr code row value my api url is present after the successfully fetching data i want to redirect to registration page but the following error is comming
TypeError: this.router.navigate is not a function
i checked all the module , routing module all imported , exported correctly
there is a question related to this and i checked all the fixes are already appiled
i imported all required module,component
here is my barcode-scanning.page.ts
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
import { Component, NgZone, OnInit } from '@angular/core';
import { UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import { DialogService } from 'src/app/services/dialog.service';
import { FilePicker } from '@capawesome/capacitor-file-picker';
import { BarcodeScanningModalComponent } from './barcode-scanning-modal.component';
import { NavController } from '@ionic/angular';
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { NavigationExtras } from '@angular/router';
import {
Barcode,
BarcodeFormat,
BarcodeScanner,
LensFacing,
} from '@capacitor-mlkit/barcode-scanning';
@Component({
selector: 'app-barcode-scanning',
templateUrl: './barcode-scanning.page.html',
styleUrls: ['./barcode-scanning.page.scss'],
})
@Injectable()
export class BarcodeScanningPage implements OnInit {
public readonly barcodeFormat = BarcodeFormat;
public readonly lensFacing = LensFacing;
public formGroup = new UntypedFormGroup({
formats: new UntypedFormControl([]),
lensFacing: new UntypedFormControl(LensFacing.Back),
googleBarcodeScannerModuleInstallState: new UntypedFormControl(0),
googleBarcodeScannerModuleInstallProgress: new UntypedFormControl(0),
});
public barcodes: Barcode[] = [];
public isSupported = false;
public isPermissionGranted = false;
constructor(
private readonly dialogService: DialogService,
private readonly ngZone: NgZone,
private http: HttpClient,
private router: Router,
private navCtrl: NavController
) {}
public ngOnInit(): void {
BarcodeScanner.isSupported().then((result) => {
this.isSupported = result.supported;
});
BarcodeScanner.checkPermissions().then((result) => {
this.isPermissionGranted = result.camera === 'granted';
});
BarcodeScanner.removeAllListeners().then(() => {
BarcodeScanner.addListener(
'googleBarcodeScannerModuleInstallProgress',
(event) => {
this.ngZone.run(() => {
console.log('googleBarcodeScannerModuleInstallProgress', event);
const { state, progress } = event;
this.formGroup.patchValue({
googleBarcodeScannerModuleInstallState: state,
googleBarcodeScannerModuleInstallProgress: progress,
});
});
}
);
});
}
public async startScan(): Promise<void> {
const formats = this.formGroup.get('formats')?.value || [];
const lensFacing =
this.formGroup.get('lensFacing')?.value || LensFacing.Back;
const element = await this.dialogService.showModal({
component: BarcodeScanningModalComponent,
cssClass: 'barcode-scanning-modal',
showBackdrop: false,
componentProps: {
formats: formats,
lensFacing: lensFacing,
},
});
element.onDidDismiss().then((result) => {
const barcode: Barcode | undefined = result.data?.barcode;
if (barcode) {
this.processBarcode(barcode.rawValue);
}
});
}
private processBarcode(rawValue: string | undefined): void {
if (!rawValue) {
console.error('No barcode raw value to process');
return;
}
const apiUrl = rawValue;
this.http.get(`${rawValue}`)
.pipe(
catchError(this.handleError)
)
.subscribe(response => {
console.log('API response:', response);
const navigationExtras: NavigationExtras = {
state: {
data: response
}
};
this.router.navigate(['/registration'], navigationExtras);
});
}
private handleError(error: HttpErrorResponse) {
console.error('An error occurred:', error);
return throwError('Something went wrong; please try again later.');
}
trackByBarcode(index: number, barcode: Barcode): string {
return barcode.rawValue || index.toString();
}
public async readBarcodeFromImage(): Promise<void> {
const { files } = await FilePicker.pickImages({ limit: 1 });
const path = files[0]?.path;
if (!path) {
return;
}
const formats = this.formGroup.get('formats')?.value || [];
const { barcodes } = await BarcodeScanner.readBarcodesFromImage({
path,
formats,
});
this.barcodes = barcodes;
}
public async scan(): Promise<void> {
const formats = this.formGroup.get('formats')?.value || [];
const { barcodes } = await BarcodeScanner.scan({
formats,
});
this.barcodes = barcodes;
}
public async openSettings(): Promise<void> {
await BarcodeScanner.openSettings();
}
public async installGoogleBarcodeScannerModule(): Promise<void> {
await BarcodeScanner.installGoogleBarcodeScannerModule();
}
public async requestPermissions(): Promise<void> {
await BarcodeScanner.requestPermissions();
}
}