简体   繁体   中英

How to redirect page depends on condition in angular 8

I am trying to redirect the page from login page.I have one flag that is for checking the cart is empty or not. I can set that flag true or false. I want to redirect the pages after logged depends on the flag and logged or not.I have tried but not working.Please help anyone to find the solution.

login.commponent.ts:

onSubmit() {
    this.submitted = true; 
    if (this.loginForm.invalid) {
        return;
    } 
    this.loading = true;
    this.authenticationService.login(this.f.username.value, this.f.password.value)
        .pipe(first())
        .subscribe(
            data => {
              //if cart is not empty
              // if(this.flag==true){
              //   this.router.navigate(['./billing']);
              // }
               //if cart is empty
              //else{
              //   this.router.navigate(['./cartempty']);
              // }
              //this.router.navigate([this.returnUrl]);

            },
            error => {
                this.alertService.error(error);
                this.loading = false;
            });
}

order.component.ts:

goTOloginbilling(){

                  //if Not logged
                  // if(????){
                  //   this.router.navigate(['./login']);
                  // }
                  //if cart is not empty
                  // else if(this.flag==true){
                  //   this.router.navigate(['./billing']);
                  // }
                   //if cart is empty
                  //else if(this.flag==false){
                  //   this.router.navigate(['./cartempty']);
                  // }

  }

Demo: https://stackblitz.com/edit/angular-8-registration-login-example-gnjckr?file=app/order/order.component.ts

Looking your code I saw some problems.

In login.component.ts the variable flag is declared like this:

flag: true

I suppose you wanted write flag: boolean= true;

In the same file, in onSumbit function when you call the login function, after you have the response you had wrote:

this.router.navigate(['./billing']);

But In app.routing.module.ts the route is:

{ path: '', component: BillingComponent, canActivate: [AuthGuard] },

So you had to write:

this.router.navigate(['']);

So in conclusion:

onSubmit() {
    this.submitted = true;
    if (this.loginForm.invalid) {
      return;
    }
    this.loading = true;
    this.authenticationService
      .login(this.f.username.value, this.f.password.value)
      .subscribe(
        data => {
          //if cart is not empty
          if (this.flag == true) this.router.navigate([""]);
          else {
            this.router.navigate(["cartempty"]);
          }
        },
        error => {
          this.alertService.error(error);
          this.loading = false;
        }
      );
  }

Obviously you can define a route for cartEmpty in app.routing.ts :

{ path: 'cartempty', component: CartemptyComponent },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM