Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

angular - How to execute in first a function and in second canActivate()

I have this AuthGuard. In this code I want to create a condition. If my resetpasss is not null I want to navigate in here ResetPassIdComponent else I want to navigate in LoginFirstComponent.

For this I create AuthGuard.

export class AuthGuard implements CanActivate {
    myappurl: any;
    resetpasss: any;
    constructor(private router: Router, private auth: LoginService) {
    }
    geturl() {
        handleOpenURL((appURL: AppURL) => {
            console.log('Got the following appURL1', appURL);
            this.myappurl = appURL
            let url_1 = this.myappurl.toString();
            let url_id = url_1.split("/").reverse()[0];
            this.resetpasss = url_id
            let LS = require("nativescript-localstorage");
            LS.setItem(this.resetpasss)
            // this.router.navigateByUrl('/resetPasswordRequest/' + this.resetpasss);
            console.log('this.resetpass1', this.resetpasss)
        });
        return true;
    }
    canActivate(): boolean {
        this.geturl();
        if (this.auth.isAuthenticated()) {
            return true;
        }
        console.log('this.resetpasss2', this.resetpasss)
        this.router.navigate(['/outsidelogin/login']);
        return false;
    }
 }

I click a link from email, this link show me in geturl(){..}. From this function geturl(){..} I get an id and save in localstorage. Now in canActivate() I want to create a condition that navigate me in a component that I want.

Can you ask me any idea how to create a condition?

My routing.ts

const routes: Routes = [
  {
    path: 'home',
    component: HomeComponent,
    canActivate: [AuthGuard],
    children: [
      {
        path: 'fp', component: FirstPageComponent
      }
    ]
  },
  {
    path: 'outsidelogin',
    component: outsideloginComponent,
    children: [
      { path: 'login', component: LoginFirstComponent },
      { path: 'register', component: RegisterComponent },
    ]
  },
    { path: '', redirectTo: '/home/fp', pathMatch: 'full' },
    { path: 'resetPasswordRequest/:id', component: ResetPassIdComponent }
];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can add the condition in CanActivate() itself. Can you tried like the below?

canActivate(): boolean {
    this.geturl();
    if (this.auth.isAuthenticated()) {
        return true;
    } else if(this.resetpasss){
       this.router.navigate([this.myappurl]);
    }else{
        this.router.navigate(['/outsidelogin/login']);
    }       
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...