简体   繁体   English

router.navigateByUrl不发送值Angular

[英]router.navigateByUrl don't send value Angular

I am trying send 1 value between components Angular ... but when I arrive in my component I don't receive nothing. 我正在尝试在组件Angular之间发送1值...但是当我到达组件时,我什么也没收到。

Component1: 组件1:

onCellClicked(event) {
    if (event.colDef.field === 'dni') {
      console.log('dni en mi componente : ', event.value);
      this.router.navigateByUrl('probarborrar/', event.value);
    }
  }

First check if the Cell is dni, then I show "console.log(event.value)" It is true, I can see my dni. 首先检查Cell是否为dni,然后显示"console.log(event.value)" 。是的,我可以看到我的dni。 Finally I go the second component. 最后,我讲第二部分。

Routing. 路由。

     {
        path: 'probarborrar/:idUser',
        component: ProbandoBorrarComponent,
        data: {
          breadcrumb: 'probar'
        }
      }

Component2. 组件2。

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-probando-borrar',
  templateUrl: './probando-borrar.component.html',
  styleUrls: ['./probando-borrar.component.scss']
})
export class ProbandoBorrarComponent implements OnInit {
  public dni;

  constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    this.dni= this.route.snapshot.paramMap.get('idUser');
    console.log('en probando es :', this.dni); -->nothing
  }
}

html html

<p style="color:white;background-color:black;">
  probando-borrar works! : {{ dni }}
</p>

I can arrive Component2 but I don't receive nothing. 我可以到达Component2,但什么也没收到。

在此处输入图片说明

You should be using the navigate function for this. 您应该为此使用导航功能。 Apparently navigateByUrl has an issue with queryParams . 显然navigateByUrl有一个与queryParams问题 Secondly, you need to pass a navigationExtras object to the navigate function. 其次,你需要一个传递navigationExtras对象的navigate功能。

Checkout the code sample. 签出代码示例。

this.router.navigate(['probarborrar'], {queryParams: {idUser: event.value}});

In component2, retrieve the value like 在component2中,检索类似的值

constructor(private activatedRoute: ActivatedRoute) {}

ngOnInit() {
  this.activatedRoute.queryParams.subscribe((params) => {
      console.log(params['idUser']);
  });
}

Working Demo: https://stackblitz.com/edit/angular-ytfgp8 工作演示: https : //stackblitz.com/edit/angular-ytfgp8

Try this. 尝试这个。

import { map } from 'rxjs/operators' ; import { map } from 'rxjs/operators' ;

ngOnInit() {
    this.dni = this.route
      .queryParamMap
      .pipe(map((params) => {
        const param = params.get('idUser');
        console.log(param);
        return param;
      }
      ));
  }

Read more about routing and naviagtion in here . 此处阅读有关路由和导航的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular中router.navigateByUrl和router.navigate的使用方法 - How to use router.navigateByUrl and router.navigate in Angular router.navigateByUrl()导致导航到根并重新加载,角度为2.4.7 - router.navigateByUrl() causes navigation to root and reload, angular 2.4.7 如果页面仍在加载,Angular router.navigateByUrl()无法正常工作 - Angular router.navigateByUrl() not working if page is still loading 如何使用 Karma 和 Jasmine 在 Angular 中模拟 router.navigateByUrl(...).then - How to mock router.navigateByUrl(…).then in Angular with Karma and Jasmine routerLinkActive 不适用于 router.navigateByUrl 或导航 - routerLinkActive does not work with router.navigateByUrl or navigate 将router.navigateByUrl之后的组件刷新为具有不同参数的相同URL - Refresh component after router.navigateByUrl to the same URL with different parameters 如何在使用 Router.navigateByUrl 时将数据传递给组件 - How to pass data to a component while using Router.navigateByUrl 使用Angular 7的NavigateByUrl发送数据 - to send data with NavigateByUrl of Angular 7 Angular Router:用false解析的navigationByUrl() - Angular Router: navigateByUrl() resolving with false CanDeactive Guard:通过 router.navigateByUrl() 路由时如何重置活动 class - CanDeactive Guard: How to reset active class when routing by router.navigateByUrl()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM