我试图从RC1到RC4迁移和我有麻烦的查询字符串参数。 ActivatedRoute对象总是空的。
hero.component.ts
import {Component, OnInit} from "@angular/core";
import {Control} from "@angular/common";
import {ROUTER_DIRECTIVES, ActivatedRoute} from '@angular/router';
@Component({
template: '../partials/main.html',
directives: [ROUTER_DIRECTIVES]
})
export class HeroComponent implements OnInit {
constructor(private _activatedRoute: activatedRoute) {
}
ngOnInit() {
this._activatedRoute.params.subscribe(params => {
console.log(params);
});
}
}
main.ts
import {bootstrap} from '@angular/platform-browser-dynamic';
import {HTTP_PROVIDERS, RequestOptions, Http} from '@angular/http';
import {AppRouterProviders} from './app.routes';
bootstrap(AppComponent, [
AppRouterProviders,
HTTP_PROVIDERS
]);
app.component.ts
import {Component, OnInit} from '@angular/core';
import {HeroComponent} from './hero.component';
import {RouteConfig, Router, ROUTER_DIRECTIVES} from '@angular/router';
@Component({
selector: 'my-app',
templateUrl: '../partials/main.html',
directives: [
HeroComponent,
ROUTER_DIRECTIVES
]
})
export class AppComponent {
}
谐音/ main.html中
<a class="nav-link" [routerLink]="['/']" [queryParams]="{st: 'new'}">New</a>
app.routes.ts
import {provideRouter, RouterConfig} from '@angular/router';
import {HeroComponent} from './hero.component';
import {ErrorComponent} from './error.component';
const routes: RouterConfig = [
{path:'', component: HeroComponent},
{path:'**', component: ErrorComponent}
];
export const AppRouterProviders = [
provideRouter(routes)
];
当我点击链接“新建”控制台打印出空对象
对象{}
更新
plunker
更新(2.0.0最终)
( somepath/:someparam/someotherpath
),你可以使用订阅这些_router.queryParams.subscribe(...)
export class HeroComponent implements OnInit {
constructor(private _activatedRoute: ActivatedRoute, private _router:Router) {
_activatedRoute.queryParams.subscribe(
params => console.log('queryParams', params['st']));
原版的
如果你想queryParams
代替路线PARAMS( somepath/:someparam/someotherpath
),你可以使用订阅这些_router.routerState.queryParams.subscribe(...)
export class HeroComponent implements OnInit {
constructor(private _activatedRoute: ActivatedRoute, private _router:Router) {
_router.routerState.queryParams.subscribe(
params => console.log('queryParams', params['st']));
Plunker例子
我用了很长时间了同样的问题挣扎。 答案是棘手的,在文档中有人指出:
ActivatedRoute:被提供给一个包含路线特定信息,例如路由参数,静态数据,解析数据,全球查询参数,并且全局片段的每个路由组分A的服务。 这是中提到这里
诀窍答案是“路线组件”,这意味着ActivatedRoute将只对路由组件。 换句话说只部件在路由表中说明。
>阅读混淆官方文档在这里 。 它提到“与出口加载的组件相关联的路线”。 我们只能猜测是出口...
我写了一个小调查代码在单机app.module.ts研究这个问题:
import { Component, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes, ActivatedRoute } from '@angular/router';
@Component({
selector: 'comp1',
template: `<h4>comp1 works</h4>`,
})
export class Comp1 { }
@Component({
selector: 'comp2',
template: `<h4>comp2 works</h4>`,
})
export class Comp2 { }
@Component({
selector: 'fail',
template: `<div>Wrong URL:{{url | json}}</div> <h4>Failure page works</h4>`,
})
export class Fail {
private url: any;
constructor(public activeRoute: ActivatedRoute) {
this.url = activeRoute.snapshot.url
};
}
@Component({
selector: 'app-root',
template: `<div>Base URL:{{url | json}}</div> <router-outlet></router-outlet>`,
})
export class App {
private url: any;
constructor(public activeRoute: ActivatedRoute) {
this.url = activeRoute.snapshot.url
};
}
const appRoutes: Routes = [
{ path: '1', component: Comp1 },
{ path: '2', component: Comp2 },
{ path: '**', component: Fail }
];
@NgModule({
imports: [BrowserModule, RouterModule.forRoot(appRoutes)],
declarations: [App, Comp2, Comp1, Fail],
bootstrap: [App]
})
export class AppModule { }
尝试运行以下URI的模块:
- HTTP://本地主机:2分之4200
- HTTP://本地主机:1分之4200
- HTTP://本地主机:4200 / A / B / C
祝你好运,希望代码是示范。
检查角文档,它都在那里: https://angular.io/docs/ts/latest/guide/router.html
constructor(
private route: ActivatedRoute,
private router: Router,
private service: HeroService) {}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
let id = +params['id']; // (+) converts string 'id' to a number
this.service.getHero(id).then(hero => this.hero = hero);
});