को बनाए रखते हुए कैनलोड गार्ड पर पूर्ण पथ कैसे प्राप्त करें मैं कोणीय 2.4 संस्करण और राउटर संस्करण "^ 3.4.10" का उपयोग कर रहा हूं।कोणीय 2: रीडायरेक्ट यूआरएल
मैं प्रमाणन गार्ड सेवा का उपयोग यूआरएल अनुप्रेषित को संभालने के लिए कोशिश कर रहा हूँ।
उपयोगकर्ता मारा जब url 'डोमेन/काम/3/विस्तार' और यदि उपयोगकर्ता तो लॉग इन नहीं कर रहा है कि उपयोगकर्ता ने 'डोमेन/लॉगिन' पृष्ठ पर पुनः निर्देशित। और जब उपयोगकर्ता को सफलतापूर्वक प्रणाली फिर पिछले यूआरएल उपयोगकर्ता का उपयोग करने की कोशिश करता है जो करने के लिए 'डोमेन/काम/3/विस्तार' पुनः निर्देशित करने के लिए लॉग इन करें।
मैं काम मॉड्यूल पर CanLoad गार्ड को लागू किया है। तो उपयोगकर्ता url 'डोमेन/काम/3/विस्तार' का उपयोग करने की कोशिश करता है और उपयोगकर्ता के लिए लॉग इन नहीं है, तो जब, authservice की redirectUrl संपत्ति (this.authService.redirectUrl) में यूआरएल भंडार।
इसलिए यहाँ मुद्दा मेरे मामले में आता है। मैं उस यूआरएल का पूरा मार्ग प्राप्त नहीं कर पा रहा हूं जिसे उपयोगकर्ता ने मारा था। मुझे कैनलोड गार्ड के भीतर 'असाइनमेंट' के बजाय 'असाइनमेंट/3/विवरण' मिल रहा है। मैं पूर्ण पथ कैसे प्राप्त कर सकता हूं ताकि मैं उपयोगकर्ता को कैनलोड गार्ड के भीतर उचित पथ पर रीडायरेक्ट कर सकूं।
CanLoad:
canLoad(route: Route): boolean {
let url = `/${route.path}`; // here i got url path 'assignment' instead 'assignment/3/detail'
return this.checkLogin(url);
}
मुख्य मार्ग app.routes.ts
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent},
{
path: 'assignment',
loadChildren: './assignment/assignment.module#AssignmentModule',
canLoad: [AuthGuard]
},
{ path: '**', redirectTo: '', pathMatch: 'full' }];
असाइनमेंट मार्ग: काम-routing.ts
const assignmentRoutes: Routes = [
{
path: '',
component: AssignmentComponent,
canActivate: [AuthGuard]
children: [
{
path: '',
canActivateChild: [AuthGuard],
children: [
{
path: ':assignmentId/detail', component: AssignmentDetailComponent,
canActivate: [AuthGuard]
}
]
}]
}];
AuthGuard: Auth-gurad.service.ts
import { Injectable } from '@angular/core';
import {
CanActivate, Router,
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanActivateChild,
NavigationExtras,
CanLoad, Route
} from '@angular/router';
import { AuthService } from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
constructor(private authService: AuthService, private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
let url: string = state.url;
return this.checkLogin(url);
}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.canActivate(route, state);
}
canLoad(route: Route): boolean {
let url = `/${route.path}`; // here i got url path 'assignment' instead 'assignment/3/detail'
return this.checkLogin(url);
}
checkLogin(url: string): boolean {
if (this.authService.isLoggedIn) {
if(this.authService.redirectUrl!=null){
let redirectUrl = this.authService.redirectUrl;
this.authService.redirectUrl = null;
this.this.router.navigate([redirectUrl]);
}
return true;
}
// Store the attempted URL for redirecting
this.authService.redirectUrl = url;
// Navigate to the login page
this.router.navigate(['/login']);
return false;
}
}
धन्यवाद आपके सुझाव और उत्तर के लिए एडविन। – Krishna