我有一個主要組件,它有一個路由器插座。在是在路由器出口裝的分量我搶的URL參數是這樣的:父組件從ActivatedRoute獲取空Params
ngOnInit(): void {
// _route is injected ActivatedRoute
this._route.params.subscribe((params: Params) => {
if(params['url']){
this.testUrl = params['url'].replace(new RegExp('\%2[fF]', 'g'), '/');
}
});
}
這工作得很好,但是當我嘗試在我的頂級組件params對象始終是空的。我不明白爲什麼嵌套組件param對象中有數據,我試圖以同樣的方式訪問它。沒有錯誤要繼續,param對象只是空的。
爲什麼我的父組件不能從ActivatedRoute獲得正確的Params對象?
編輯:
的要求充分父組件爲app.module.ts
import { OnInit, Component, Directive } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
})
export class AppComponent {
public testUrl: string;
constructor(private router: Router, private _route: ActivatedRoute) {
}
ngOnInit(): void{
this._route.queryParams.subscribe((params: Params) => {
console.log(params);
if (params['url']) {
this.testUrl = params['url'].replace(new RegExp('\%2[fF]', 'g'), '/');
alert(params['url']);
}
});
}
}
路由器的代碼:
RouterModule.forChild([
{ path: 'report', component: MobileReportComponent }
:用於嵌套模塊
RouterModule.forRoot([
{ path: '', component: CheckMobileComponent },
{ path: '**', component: CheckMobileComponent }
]),
路由器的代碼
那裏沒有直接指定我的app.component的路由,因爲它是由index.html中的選擇器加載的。
分享你的父組件 –
的代碼@Sid更新問題 – Guerrilla
請添加你的路線代碼太 –