Reputation: 99
I'm using nativescript-urlhandler in my Nativescript Aplication. When I put a router, my application routing in first in Main Component and in second in component that I want.
I want to routing directly to component that I want.
I have this routing.ts code:
const routes: Routes = [
{
path: 'home',
component: HomeComponent,
canActivate: [AuthGuard],
children: [
{
path: 'fp', component: FirstPageComponent
},
{
path: 'profile', component: ProfileComponent
}
]
},
{
path: 'outsidelogin',
component: outsideloginComponent,
children: [
{ path: 'login', component: LoginFirstComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'resetPasswordRequest/:id', component: ResetPassIdComponent }
]
},
{ path: '', redirectTo: '/home/fp', pathMatch: 'full' }
];
AuthGuard
canActivate(): boolean {
if (this.auth.isAuthenticated()) {
return true;
}
this.router.navigate(['/outsidelogin/login']);
return false;
}
In this code is a problem.
ngOnInit() {
if (this.auth.isAuthenticated()) {
return true;
}
handleOpenURL((appURL: AppURL) => {
console.log('Got the following appURL', appURL);
this.myappurl = appURL
let url_1 = this.myappurl.toString();
let url_id = url_1.split("/").reverse()[0];
this.resetpasss = url_id
this.router.navigateByUrl('/outsidelogin/resetPasswordRequest/' + this.resetpasss);
});
}
Upvotes: 0
Views: 219
Reputation: 21908
Use Router Guards, implement canActivate for your main component route, if you have a URL to navigate from handleOpenURL
then return false and navigate to the new url.
Update: After looking at your code, I think you must keep your ResetPassIdComponent
lightweight. Seems it's nested under multiple page router outlets, try to keep that component / copy of the component at root level for better & faster initialisation.
Replace the code below in your auth.guard.ts
and remove the url handling code from app component.
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { LoginService } from '~/services/login';
import { handleOpenURL, AppURL } from 'nativescript-urlhandler';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private auth: LoginService) { }
canActivate(): boolean {
if (this.auth.isAuthenticated()) {
return true;
}
const timeOut = setTimeout(() => {
handleOpenURL(null);
this.router.navigate(['/test/login']);
}, 100);
handleOpenURL((appURL: AppURL) => {
if (timeOut) {
clearTimeout(timeOut);
console.log('Got the following appURL', appURL);
let url_1 = appURL.toString();
let url_id = url_1.split("/").reverse()[0];
console.log(url_id);
this.router.navigateByUrl('/test/questions/' + url_id);
}
});
return false;
}
}
Upvotes: 1