我使用angular 2.0.0
,我有一个像这样的URL:
http://localhost:4200/?sptoken=MY_TOKEN#/resetPassword/
我想从中获得MY_TOKEN
。我尝试了我能找到的所有东西,但我只得到“undefined
”。
第二个问题是我使用hashtag位置策略,如果我访问这样的URL,它会转换为http://localhost:4200/#/resetPassword/(查询字符串消失),我唯一可以访问此令牌的时间是在主要组件转换之前但是我不知道怎么弄它,我发现的大部分内容都是指矩阵表示法查询参数。
您对我如何解决这个问题有什么建议吗?
这是我的代码:
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']);
}
}
我的路线:
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: ''
});
我也试图通过使用这个URL尝试获取主要组件中的令牌:http://localhost:4200/?sptoken=MY_TOKEN#
但它发生相同
这可能是因为重定向。不会为重定向保留查询参数。而不是重定向添加虚拟组件,在组件中从激活的路径获取查询参数,然后使用router.navigate()
进行重定向