ionic4 app exit confirmation alert message and conditional routing


export class AppComponent implements OnInitOnDestroyAfterViewInit
declare   backButtonSubscription: any;
in implements required this three::: OnInitOnDestroyAfterViewInit
ngOnInit() { }
 ngAfterViewInit() { 
    this.backButtonSubscription = this.platform.backButton.subscribe(() => {
    this.presentAlertConfirm();
    });
  }
async presentAlertConfirm() {
    const alert = await this.alertController.create({
      header: 'Exit App?',
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah=> {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: 'Okay',
          handler: () => {
            navigator['app'].exitApp();
          }
        }
      ]
    });
    await alert.present();
  }
  ngOnDestroy() { 
    this.backButtonSubscription.unsubscribe();
  }
conditional routing
initializeApp() {
    this.platform.ready().then(() => {
      this.storage.get('SignInData').then((resp=> {
        this.data=resp.isLoggedIn;
        console.log(resp.isLoggedIn);
        if(this.data) { 
          this.router.navigateByUrl('/tabs');    
        } else { 
         this.router.navigateByUrl('/');     
        }
      })   
     this.statusBar.styleDefault();
      this.splashScreen.hide();
    });
  }


service calling


import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { throwError } from 'rxjs';
import { map, filter, catchError, mergeMap } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
})
export class ApiService {
API_HOST: "http://199.82.98.3:3001";/*production*/
public slidescall = "login.php";
constructor(private http: HttpClient) {

}


serviceCall(formData: any, url:any) {
console.log("signupAPICall::DATA", formData);
const reqHeader = new HttpHeaders({ 'Authorization': 'Bearer ' });
reqHeader.append('Content-Type', 'application/json');
return this.http.post(this.API_HOST + url, formData)
.pipe(map(res => res), catchError(this.errorHandler));
}
serviceCallWithoutParam(url:any) {
console.log("signupAPICall::DATA",this.API_HOST+url);
const reqHeader = new HttpHeaders({ 'Authorization': 'Bearer ' });
reqHeader.append('Content-Type', 'application/json');
return this.http.get("http://183.82.98.3:3001" + url)
.pipe(map(res => res), catchError(this.errorHandler));
}
errorHandler(error: Response) {
console.log("RESULT:::", error);
return throwError(error);
}

}

this.api.serviceCallWithoutParam({
name:this.name
})
.subscribe((result: any) => {
console.log(result);
this.result=result.data;
}).error((err)=>{
console.log(err);
})

navparams through routing
this.navCtrl.navigate('/fullproductdetails',
{"catproducts":this.particularProducts,"catId":catId});


private _Activatedroute: ActivatedRoute,
this.sub = this._Activatedroute.params.subscribe(params => {
this.id = params['id'];
localStorage.setItem("catid",this.id.toString());
this.searchText=params['search'];
this.productService.getSearch(this.searchText)
.subscribe(result=>
{
if(result[0]=="No"){
this.avail=true;
}else {
this.avail=false;
}
console.log(result);
}
);
});









Comments

Popular posts from this blog

How to Add Firebase Analytics to Your NativeScript Mobile App

sidemenu refresh through events

push multiple data in array(select and disselect items in array)