Próbuję wysłać element potomny(resultadoBusqueda.component) seria parametrów w momencie kliknięcia przycisku komponentu, jeśli wykonuję "this.router.navigate ([" ./ resultadoBusqueda "], navigationExtras);" nie działa, a jeśli zrobię to za pomocą routera "Child One", to działa i ładuje komponent podrzędny, problem polega na tym, że przy pomocy routera nie mogę wypróbować szeregu parametrów, aby wysłać je później, muszę to zrobić, gdy z routerem. nawigować. Dowolny pomysł?
Busqueda.component.html
<button (click)="EnviarQueryParams()">Enviar query params a resultadoBusquedaComponent.ts</button>
<!--<a [routerLink]="["./resultadoBusqueda"]">Child One</a>-->
<router-outlet></router-outlet>
Busqueda.component.ts
import { Component } from "@angular/core";
import {Router, NavigationExtras} from "@angular/router";
@Component({
selector: "app-busqueda-component",
templateUrl: "./busqueda.component.html",
})
export class BusquedaComponent {
public constructor(private router: Router) { }
EnviarQueryParams(){
let navigationExtras: NavigationExtras = {
queryParams: {
"firstname": "Nic",
"lastname": "Raboy"
}
};
this.router.navigate(["./resultadoBusqueda"], navigationExtras);
}
}
resultadoBusqueda.component.ts (Komponent potomny, to obciążenie komponentu w wyjściu routera "Busqueda.Component"
import { Component, OnInit} from "@angular/core";
import { Observable } from "rxjs";
import { Subject } from "rxjs/Subject"
import {ActivatedRoute} from "@angular/router";
@Component({
selector: "app-resultado-busqueda-component",
templateUrl: "./resultadoBusqueda.component.html",
providers: [EmployeeService]
})
export class ResultadoBusquedaComponent implements OnInit {
public firstname: string;
public lastname: string;
constructor(private _employeeService: EmployeeService,private route:
ActivatedRoute) { }
ngOnInit() {
this.route.queryParams.subscribe(params => {
this.firstname = params["firstname"];
this.lastname = params["lastname"];
});
}
}
app.module.ts
// Some stuff ...
const appRoutes: Routes = [
{ path: "busqueda", component: BusquedaComponent,
children: [{path: "resultadoBusqueda", component:
ResultadoBusquedaComponent}]
},
{ path: "employees", component: EmployeeListComponent },
{ path: "", redirectTo: "/home", pathMatch: "full" },
{ path: "**", component: PageNotFoundComponent }
];
// Some stuff ...
imports: [
BrowserModule, FormsModule, Ng2CompleterModule, HttpModule,
RouterModule.forRoot(appRoutes)
],
Odpowiedzi:
0 dla odpowiedzi № 1Można przekazywać parametry zapytania za pomocą RouterLink
także. To jest dyskutowane tutaj w Angularnych dokumentach.
Możesz dodać parametry zapytania w ten sposób:
<a routerLink="resultadoBusqueda" [queryParams]="navigationExtras">Child One</a>