Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2.0.0-从url标签位置策略获取查询字符串(带?)_Angular_Query Parameters_Angular2 Router - Fatal编程技术网

Angular2.0.0-从url标签位置策略获取查询字符串(带?)

Angular2.0.0-从url标签位置策略获取查询字符串(带?),angular,query-parameters,angular2-router,Angular,Query Parameters,Angular2 Router,我使用的是angular 2.0.0,我的URL如下: 我想从它那里获得我的\u令牌。我尝试了所有我能在这里找到的东西,但我只得到了“未定义的”” 第二个问题是我使用了hashtag定位策略,如果我像这样访问它被转换到的URL(查询字符串消失),我唯一可以访问这个令牌的时间是在它被转换之前的主组件中,但我不知道如何获取它,我发现的大部分内容都涉及矩阵表示法查询参数 你对我如何解决这个问题有什么建议吗 这是我的代码: export class ResetPasswordComponent imp

我使用的是angular 2.0.0,我的URL如下:

我想从它那里获得
我的\u令牌
。我尝试了所有我能在这里找到的东西,但我只得到了“
未定义的”

第二个问题是我使用了hashtag定位策略,如果我像这样访问它被转换到的URL(查询字符串消失),我唯一可以访问这个令牌的时间是在它被转换之前的主组件中,但我不知道如何获取它,我发现的大部分内容都涉及矩阵表示法查询参数

你对我如何解决这个问题有什么建议吗

这是我的代码:

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()

执行重定向。这可能是因为重定向。不为重定向保留查询参数。不是重定向,而是在组件中添加一个虚拟组件,从激活的路由获取查询参数,然后使用
router.navigate()

sptoken
进行重定向。是否使用
queryParams
进行设置,我使用外部api进行身份验证和密码重置,他们通过query params
sptoken
向我发送令牌。您是否将其设置为
queryParams
??是,我使用外部api进行身份验证和密码重置,他们通过查询参数向我发送令牌。我尝试过这样做,但由于HashLocationStrategy,它无法识别查询字符串,当我不使用它时,它可以工作,但我必须使用它。我不认为这与
HashLocationStrategy
有关
HashLocationStrategy
的工作原理与
PathLocationStrategy
相同,只是URL表示不同(后者需要服务器支持)。也许你看错了层次。Queryparameters仅在顶级路由上可用。我只有顶级路由,我甚至在主应用程序组件中也尝试过,但仍然无法获取包含查询字符串的URL是什么样子的?您可以编辑您的问题并在那里添加演示您尝试的代码吗?请尝试
this.route.queryParams.forEach((p)=>console.log(p['sptoken'])
我尝试过这样做,但由于HashLocationStrategy,它无法识别查询字符串,当我不使用它时,它可以工作,但我必须使用它。我认为这与
HashLocationStrategy
无关
HashLocationStrategy
的工作原理与
PathLocationStrategy
相同,只是URL表示不同(后者需要服务器支持)。也许你看错了层次。Queryparameters仅在顶级路由上可用。我只有顶级路由,我甚至在主应用程序组件中也尝试过,但仍然无法获取包含查询字符串的URL是什么样子的?您可以编辑您的问题并在那里添加演示您尝试的代码吗?请尝试
this.route.queryParams.forEach((p)=>console.log(p['sptoken'])