我正在使用角度2版rc4和路由器版本3.0.0-beta.2。
鉴于此网址:http://localhost:8888/#/test;status=07 我想获取status可选参数的值。 据我所知,这段代码应该有效:
console.log("status", this._activatedRoute.snapshot.params['status']);
但我对此没有定义。 有趣的是,这段代码给了我正确的价值:
console.log("urlsegment", this._activatedRoute.snapshot['_urlSegment'].pathsWithParams[0].parameters['status']);
我不明白为什么? 我做错了什么? 第二种解决方案非常难看,所以我真的不想使用它。
欢迎任何帮助。
编辑: main.ts:
bootstrap(App, [
APP_ROUTER_PROVIDERS, ..Some other stuff..]).then((appRef) => AppInjector(appRef.injector));
app.routes.ts:
export const routes: RouterConfig = [
{path: 'admin', component: FrontComponent, children: [...FrontRoutes]},
{path: '', component: PublicComponent, children: [ ...PublicRoutes]}
];
export const APP_ROUTER_PROVIDERS = [
provideRouter(routes)
];
public.routes.ts:
export const PublicRoutes: RouterConfig = [
{path: '', redirectTo: '/dashboard', terminal: true, canActivate:[CanActivateGuard]},
{path: 'test', component: TestRouterComponent, children: [...TestRoutes]},
{path: 'dashboard', component: DashboardRouterComponent, children: [...DashboardRoutes]},
];
test.routes.ts:
export const TestRoutes: RouterConfig = [
{path: '', component: TestComponent, canActivate:[CanActivateGuard]},
{path: 'detail/:id', component: TestDetailComponent, canActivate:[CanActivateGuard]},
];
我使用路由器导航的方式:
this._router.navigate(['test', {status:07}]);
答案 0 :(得分:0)
可选参数添加到特定路线。如果将它们添加到父路线或子路线中,您将无法获得它们。
_activatedRoute.parent.queryParams.subscribe(params => console.log(params['status']));, _activatedRoute.firstChild.queryParams.subscribe(params => console.log(params['status']));
parent
,firstChild
,children
是RC.6中添加的便捷方法