Reputation: 71
I use angular 2.0.0
and I have an URL like this:
http://localhost:4200/?sptoken=MY_TOKEN#/resetPassword/
I want to get MY_TOKEN
from it. I tried everything i could find here but I only get "undefined
".
The second problem is that I use hashtag location strategy and if i access the URL like this it gets transformed to http://localhost:4200/#/resetPassword/ (the query string is gone), the only time when i could access this token is in the main component before it gets transformed but I don't know how to get it, most of the things I found are referring to the matrix notation query parameters.
Do you have any suggestions on how I can solve this?
This is my code:
export class ResetPasswordComponent implements OnInit {
constructor(private route: ActivatedRoute) {
console.log(this.route.snapshot.queryParams['sptoken']); // when i don't use
//HashLocationStrategy it logs the token
}
}
export class AppComponent implements OnInit {
constructor(private router: Router, public configService: ConfigService, private cookieService: CookieService,private route: ActivatedRoute) {
console.log(this.route.snapshot.queryParams['sptoken']);
}
}
And my routes:
const appRoutes: Routes = [
{path: UrlPaths.HELLO, component: HelloComponent, canActivate: [PrivatePageGuard]},
{path: UrlPaths.LOGIN, component: LoginComponent},
{path: UrlPaths.MAIN_PAGE, component: AppComponent},
{path: UrlPaths.FORGOT_PASSWORD, component: ForgotPasswordComponent},
{path: UrlPaths.RESET_PASSWORD, component: ResetPasswordComponent}
];
export const appRoutingProviders: any[] = [];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
export const UrlPaths = Object.freeze({
LOGIN: 'login',
HELLO: 'hello',
FORGOT_PASSWORD: 'forgotPassword',
RESET_PASSWORD: 'resetPassword',
MAIN_PAGE: ''
});
I have also tried to try to get the token in the main component by using this URL: http://localhost:4200/?sptoken=MY_TOKEN#
but it happens the same
Upvotes: 1
Views: 1056
Reputation: 658037
This is probably because of a redirect. Query parameters are not retained for redirects. Instead of a redirect add a dummy component, in the component get the query parameters from the activated route and then do the redirect with router.navigate()
Upvotes: 0