Mam ten program w angular, gdzie do tej pory biorę kod pocztowy od użytkownika, i po naciśnięciu przycisku wysyła ten przekazanie do funkcji, gdzie jest on wysyłany w api do konwersji współrzędnych Lat i Long. oglądać poniżej:
home.component.html
<div class="center" style="margin-top:50px;">
<label for="ZipCode"><b>Zip Code</b></label>
</div>
<div class="center">
<input name="zipcode" #zipcode id="zipcode" type="text" placeholder="Enter Zip Code" maxlength="5">
</div>
<div class="center" style="margin-top:100px;">
<button class="button button1" (click)="getCoords(zipcode.value)" ><b>Retrieve Data</b></button>
</div>
oczywiście, że jest to tylko fragment kodu, ale wystarczające dla celów wyświetlania. dalej przechodzi do funkcji z api, a następnie przełącza widok na składnik/stronę stacji:
strona główna.składnik.ts
export class HomeComponent implements OnInit {
constructor(
private router: Router
){}
ngOnInit(): void {
}
getCoords(val: any){
var url = "http://www.mapquestapi.com/geocoding/v1/address?key=MYKEY&location=" + val;
fetch(url)
.then((res) => res.json())
.then((data) => {
var lat = data.results[0].locations[0].displayLatLng.lat;
var long = data.results[0].locations[0].displayLatLng.lng;
this.router.navigate(["/stations"])
})
}
}
stations.component.ts - jak widać, tutaj na razie nic nie ma, bo nie mogę zrozumieć, co robić
import { Component, Input, OnInit } from '@angular/core';
@Component({
selector: 'app-stations',
templateUrl: './stations.component.html'
})
export class StationsComponent implements OnInit {
ngOnInit(): void {
}
}
teraz wszystko działa poprawnie. sprawdziłem zmienne lat i long w dzienniku konsoli, a on zwraca lat i long po prostu świetnie. mój problem polega na tym, że muszę wysłać wartość lat i long na inny składnik/stronę do wykorzystania w obliczeniach. nie będę kłamać, mówiąc, że jestem omiótł Internetem, starając się znaleźć sposób, aby to zrobić, ale, najwyraźniej, w angular trudno to zrobić. czy ktoś ma jakieś pomysły na przekazaniu wartości lat i long inny składnik/stronie?
dataLat: lat
i błąd mówi::Argument of type '{ dataLat: any; dataLong: any; }' is not assignable to parameter of type 'NavigationExtras'. Object literal may only specify known properties, and 'dataLat' does not exist in type 'NavigationExtras'.