简体   繁体   English

带有参数 Angular 6 的路由

[英]Routes with params Angular 6

I have a component that is a button and that stores objects in a variable called Microphones .我有一个组件,它是一个按钮,将对象存储在一个名为Microphones的变量中。 And I want the button to pass the object to another component and then show details of that specific object.我希望按钮将对象传递给另一个组件,然后显示该特定对象的详细信息。 But it is not working for me.但它对我不起作用。

It is assumed that in my routes I define that the presentation component has a parameter, that parameter I passed it with the button from the line of [routerLink] .假设在我的路由中,我定义了演示组件有一个参数,我通过[routerLink]行中的按钮传递了该参数。 But in the console it appears to me that the parameter is undefined .但在控制台中,我认为该参数是undefined

button-menu.component.ts button-menu.component.ts

export class ButtonMenuComponent implements OnInit {

  @Input()Microfonos:any;
  @Input()NombreClasificacion:any;

  micObj:any

  constructor() { }

  ngOnInit() { }

}

button-menu.component.html button-menu.component.html

<div class="btn-group dropright">
    <button type="button" class="btn beige dropdown-toggle" data-toggle="dropdown" data-display="static" aria-haspopup="true" aria-expanded="false">{{NombreClasificacion}}</button>
    <div class="dropdown-menu dropdown-menu-lg-left">
        <button *ngFor="let mics of Microfonos" class="dropdown-item beige text-center" type="button" [routerLink]="['/presentacion',mics]">{{mics.nombre}}</button>
    </div>
</div>

app-routing-module.ts app-routing-module.ts

const routes: Routes = [
  {
  path:'menu',
  component: MainComponent,
  },
  {
    path:'bajo',
    component: MenuBajoComponent
  },
  {
    path:'guitarra',
    component: MenuGuitarraComponent
  },
  {
    path:'presentacion/:objMic',
    component:PresentacionMicComponent
  }
];
export class PresentacionMicComponent implements OnInit {

  objMic:any;

  constructor(private rutaActiva:ActivatedRoute) { 
    this.rutaActiva.params.subscribe( params => {
      this.objMic = params['mics'];
    });
    console.log(this.objMic);
  }

  ngOnInit() { }

}

first of all @AlexanderStaroselsky is right console.log should be in subscribe首先@AlexanderStaroselsky 是对的 console.log 应该订阅

and your button-menu.component.html is wrong too, if mics is object you should use an property of mics not pass the whole object并且您的 button-menu.component.html 也是错误的,如果 mics 是对象,您应该使用 mics 的属性而不是传递整个对象

this这个

[routerLink]="['/presentacion',**mics**]


<div class="btn-group dropright">
    <button type="button" class="btn beige dropdown-toggle" data-toggle="dropdown" data-display="static" aria-haspopup="true" aria-expanded="false">{{NombreClasificacion}}</button>
    <div class="dropdown-menu dropdown-menu-lg-left">
        <button *ngFor="let mics of Microfonos" class="dropdown-item beige text-center" type="button" [routerLink]="['/presentacion',**mics**]">{{**mics.nombre**}}</button>
    </div>
</div>

Try below code:试试下面的代码:

let navigationExtras: NavigationExtras = {
            queryParams: {
                "par1": parVal1,
                "par2": parVal2,
                "par3": parVal3

            }
        };

this.router.navigate(['/componentName'], navigationExtras)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM