[英]How to get parameter from navigateByUrl?
I do navigation using:我使用以下方法进行导航:
this.router.navigateByUrl(item.url);
Where item.url
has value:其中item.url
具有值:
orgtree/1
Route is:路线是:
{
path: "orgtree/:orgstructure",
component: OrganizationsComponent
}
Inside component I do:我做的内部组件:
ngOnInit() {
this.route.paramMap.subscribe(params => {
console.log(params);
});
}
Why I get empty params?为什么我得到空参数?
Also I have tried this:我也试过这个:
this.router.navigate(['/orgtree', 1]);
NO EFFECT没有效果
I got problem:我遇到了问题:
Route should be:路线应该是:
{
path: "orgtree",
component: OrganizationsComponent
}
You should declare your variables and then use it:你应该声明你的变量然后使用它:
import { ActivatedRoute } from '@angular/router';
constructor(private route: ActivatedRoute) {
this.route.paramMap.subscribe( params => {
this.orgstructure = params.get('orgstructure');
});
}
UPDATE:更新:
You should declare your parameters like that:你应该这样声明你的参数:
this.router.navigate(['/orgtree'], {queryParams: {orgstructure: 1}});
OR if you want to use navigateByUrl
method:或者,如果您想使用navigateByUrl
方法:
const url = '/probarborrar?id=33';
this.router.navigateByUrl(url);
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
console.log(param);
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
}
Try this code, make sure route
is an instance of ActivatedRoute
试试这段代码,确保route
是ActivatedRoute
的一个实例
Refer this link, I reproduced your scenario in Stackblitz请参阅此链接,我在 Stackblitz 中复制了您的场景
a.component.html a.component.html
<button (click)="navTo()">nav to b</button>
a.component.ts a.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
@Component({
selector: 'app-a',
templateUrl: './a.component.html',
styleUrls: ['./a.component.css']
})
export class AComponent implements OnInit {
constructor( private router: Router
) { }
ngOnInit() {
}
navTo() {
let item = {
url : `b/${Math.random()}`
};
this.router.navigateByUrl(item.url);
}
}
b.component.html b.component.html
<h1>Value in param : {{value}}</h1>
<button routerLink="/a">back to a</button>
b.component.ts b.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
@Component({
selector: 'app-b',
templateUrl: './b.component.html',
styleUrls: ['./b.component.css']
})
export class BComponent implements OnInit {
public value;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.value = this.route.snapshot.params.id;
console.log(this.route.snapshot.params.id);
}
}
app-routing.module.ts应用程序路由.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AComponent } from './a/a.component';
import { BComponent } from './b/b.component';
const routes: Routes = [
{
path: '',
component: AComponent
},
{
path: 'a',
component: AComponent
},
{
path: 'b/:id',
component: BComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Try this:尝试这个:
ngOnInit() {
this.route.paramMap.subscribe(params => {
console.log(params.get('orgstructure'));
});
}
You have to get the params by its name params.get("orgstructure")您必须通过其名称获取参数 params.get("orgstructure")
Suppose:认为:
const url = '/some-url';
In some scenarios, this will work:在某些情况下,这将起作用:
window.location.href = url;
instead of代替
this.router.navigateByUrl(url);
This is due to the way routing is designed in angular.这是由于 angular 中的路由设计方式所致。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.