2017-06-14 22 views
5

को बनाए रखते हुए कैनलोड गार्ड पर पूर्ण पथ कैसे प्राप्त करें मैं कोणीय 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; 
    } 
} 

उत्तर

2

मैं इस में ठीक उसी समस्या है। इन मुद्दों से संबंधित हैं

और वे भी जो अभी तक विलय कर नहीं किया गया है इस बारे में एक खुला पीआर

है कामकाज nd अब के लिए वहाँ आप पूरा पथ के लिए उपयोग किया जा सकता है canActivate साथ canLoad विधि को बदलने के लिए हो रहा है, निश्चित रूप से बुरी बात यह है कि आप मॉड्यूल

संपादित लोड कर रहे है: इसके अलावा के लिए आप असाइनमेंट मार्ग, प्रत्येक बच्चे मार्ग के लिए canActivate पर कॉल करने की आवश्यकता नहीं है। क्या इसे मूल मार्ग पर परिभाषित किया गया है, सभी बच्चों के मार्गों के लिए गार्ड लागू करना चाहिए।

+0

धन्यवाद आपके सुझाव और उत्तर के लिए एडविन। – Krishna

1

एक और अस्थायी बजाय canActivate साथ canLoad की जगह वैकल्पिक हल नहीं है:

आप redirectUrl = location.pathname के माध्यम से यूआरएल की दुकान और this.router.navigateByUrl(redirectUrl); के माध्यम से बाद में पुन: निर्देशित कर सकते हैं।बेशक, यदि आप उपपथ पर काम करते हैं (जैसे domain) तो आपको pathname को थोड़ा सा समायोजित करना होगा।

संबंधित मुद्दे