2016-04-16 9 views
10

Biorąc pod uwagę strukturę adresu URL (nad którą nie mam kontroli), w jaki sposób mogę pobrać fragment skrótu za pomocą Angular2?Odzyskaj fragment skrótu z adresu url z Angular2

http://your-redirect-uri#access_token=ACCESS-TOKEN

Mój router robi drogę do właściwego komponentu, ale wszystko po oauth się złomowaniem i nie mogę znaleźć fragmentu skrótu w request.params lub location.path. Stracony??

router konfiguracji:

@RouteConfig([ 
{path: '/welcome', name: 'Welcome', component: WelcomeComponent, useAsDefault: true}, 
{path: '/landing/oauth', name: 'Landing', component: LandingComponent} // this one 

])

Odpowiedz

19

Dla tych nadal poszukuje:

import { ActivatedRoute } from '@angular/router'; 

export class MyComponent { 

    constructor(
    private route: ActivatedRoute, 
) { } 

    myfunction(){ 
    this.route.fragment.subscribe((fragment: string) => { 
     console.log("My hash fragment is here => ", fragment) 
    }) 
    } 
} 
1

że ten sam problem przez serwer żądania OAuth z response_type = token i który przekierowuje %REDIRECT_URI%#access_token=:access_token&token_type=:token_type&expires_in=:expires_in.

Problem polega na tym, że domyślnie bezpośredni dostęp do podrzędnego adresu URL nie jest routowany: w twoim przypadku %BASE_URL%/landing/oauth nie zostanie przekierowany do komponentu LandingComponent.

Naprawiłem go z tej konfiguracji:

import { bootstrap } from '@angular/platform-browser-dynamic'; 
import { provide } from '@angular/core'; 
import { APP_BASE_HREF } from '@angular/common'; 
import { ROUTER_PROVIDERS } from '@angular/router'; 

import { AppComponent } from './components/app/app.component'; 

bootstrap(AppComponent, [ 
    ROUTER_PROVIDERS, 
    provide(APP_BASE_HREF, { useValue: '/' }) // this line 
]); 
Powiązane problemy