首页 > 解决方案 > 使用查询字符串参数设置 Angular 6 App Base 启动 URL

问题描述

我需要设置 Angular 6 应用程序的初始启动 URL 以包含查询字符串参数。应用程序配置取决于在启动时获取这些查询字符串参数的值。

它们必须是查询字符串,不能是路径变量。

必须遵循如下模式:localhost:4200/?name=emerfan&ip=123

我尝试过使用路由器和路径,并且尝试使用 Location 在 ngOnInit 方法中重定向 AppComponent,但无济于事。

例如

ngOnInit() {
this._location.replaceState('/?name=emer&ip=123');
}

const appRoutes: Routes = [
{ path: '?name=emerfan', component: AppComponent },
{ path: '', redirectTo: '/?name=emerfan', pathMatch: 'full'}];

第一个不起作用,因为应用程序没有重新加载queryStrings来完成配置,第二个不起作用,因为似乎配置已经创建并且在路由重定向发生时没有重新创建..

是否有设置使用查询字符串参数在 Angular 应用程序中设置基本路径?

标签: angularangular2-routingangular-router

解决方案


推荐阅读