2016-04-16 16 views
10

Với cấu trúc url này (mà tôi không có quyền kiểm soát), làm cách nào tôi có thể truy xuất đoạn băm bằng Angular2?Lấy mẩu băm từ url bằng Angular2

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

router của tôi không đường với thành phần chính xác, nhưng tất cả mọi thứ sau oauth được loại bỏ và tôi không thể tìm thấy những mảnh vỡ băm trong Request.Params hoặc location.path. Doomed ??

Router cấu hình:

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

])

Trả lời

19

Đối với những người vẫn tìm kiếm:

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

tôi đã cùng một vấn đề bằng cách yêu cầu máy chủ OAuth với response_type = token, và những người chuyển hướng đến %REDIRECT_URI%#access_token=:access_token&token_type=:token_type&expires_in=:expires_in.

Vấn đề là, theo mặc định, quyền truy cập trực tiếp vào url phụ không được định tuyến: trong trường hợp của bạn, %BASE_URL%/landing/oauth sẽ không được chuyển hướng đến thành phần LandingComponent.

tôi cố định nó với cấu hình này:

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 
]); 
Các vấn đề liên quan