angular - Angular 6将查询参数传递给根
问题描述
在我app.routing.module.ts
的配置中,我配置了以下路线:
{
path: '',
redirectTo: 'dashboard',
pathMatch: 'full',
}, {
path: '',
component: AdminLayoutComponent,
children: [{
path: '',
loadChildren: './admin/admin.module#AdminModule'
}]
}
我想将一个可选的查询参数传递给应用程序的根,在我的例子中是DashboardComponent
.
在我的AdminModule
我配置我的路线是这样的:
const routes: Routes = [
{path: '', component: DashboardComponent, pathMatch: 'full', canActivate: [AuthGuard]},
{path: '/:id', component: DashboardComponent, pathMatch: 'full', canActivate: [AuthGuard]},
{path: 'users', component: UsersComponent, canActivate: [AuthGuard]},
];
如果我在仪表板组件中执行此操作,我似乎无法访问该 id 参数:
this.route.params.subscribe(
params => console.log(params) // I get nothing here
);
关于如何实现这一目标的想法?
解决方案
这就是我能够做到的方式:
只需像这样导航:
<span class="cursor-pointer" [routerLink]="['/']" [queryParams]="{ 'id': 82860}">Edit</span>
并Root Component
像这样得到它:
this.route.snapshot.queryParamMap.get('id')
我们不需要为查询参数定义路由。它会像这样出现在您的网址中:http://localhost:4200/?id=82860
.
推荐阅读
- android - 如何选择 SQL 数据库中从某一点到最后的行(在 android 中)?
- google-analytics - 将统计数据保存到 Google Analytics
- rate-limiting - 如何在 Envoy 中配置本地限速器?
- xamarin - 将 View 定位在 RelativeLayout 下面
- string - 在飞镖中获取字符串的前几个单词
- html - Angular:安全地嵌入标记保留样式属性
- angular - 处理测验中的多个单选按钮并且还面临找不到路径控制:
- python - 我如何添加一个倒计时的计时器,当它用完时它会重新启动我的程序
- python - 具有两类预测变量的 svm 预测误差
- java - WebFlux Swagger (Open API) 集成 - 发布请求示例