简体   繁体   English

如何从 navigateByUrl 获取参数?

[英]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);

Please, see work demo at stackblitz. 请参阅 stackblitz 的工作演示。

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试试这段代码,确保routeActivatedRoute的一个实例

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.

相关问题 使用 navigateByUrl() 传递查询参数 - Passing query parameter by using navigateByUrl() Angular 路由器:从navigatebyurl 返回的promise 被忽略 - Angular router: promise returned from navigatebyurl is ignored 从父到子路由的 navigateByUrl 不起作用 - navigateByUrl from parent to child route not working 如何通过navigateByUrl传递值(索引) - How to pass value(index) through navigateByUrl 如何强制 navigateByUrl() 在 Angular 9 中导航域根目录? - How to force navigateByUrl() to navigate the domain root in Angular 9? 如何检索通过 NavigateByUrl 传递给 Angular 路由的 queryParams - How to retrieve queryParams passed to an Angular route via NavigateByUrl 如何在使用 Router.navigateByUrl 时将数据传递给组件 - How to pass data to a component while using Router.navigateByUrl Angular 9 - 如何通过路由器 mocking 测试调用 navigateByUrl 的服务 - Angular 9 - how to test a service that calls navigateByUrl by mocking the router 如何使用 Karma 和 Jasmine 在 Angular 中模拟 router.navigateByUrl(...).then - How to mock router.navigateByUrl(…).then in Angular with Karma and Jasmine Angular中router.navigateByUrl和router.navigate的使用方法 - How to use router.navigateByUrl and router.navigate in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM