如何解决带有Angula的NativeScript将参数传递给url
我试图将参数传递给URL,并基于传递给URL的参数显示信息。我有一个省列表,我想做的是,当用户单击某个特定省时,我要将该省并传递到URL并显示属于所单击省的那个区。我不确定自己做错了什么,当我按下某个省时,什么也不会发生,也不会加载。
示例网址:https://example.com/province/?province_name=aprovince
services.ts
export class DistrictService {
districtArray: Array<ProvinceData> = [];
url = "https://example.com/province";
headers = new HttpHeaders({
"Content-Type": "application/json",Authorization: "Token 5a72afc446dd4c38e5f99"
});
constructor(private http: HttpClient) { }
getInfo(): any {
return this.http.get(this.url,{headers: this.headers});
}
getProvince(province: string){
return this.http.get<ProvinceData>(`${this.url}${province}/`,{headers: this.headers});
}
}
@Component({
selector: "ns-district",templateUrl: "./district.component.html",styleUrls: ["./district.component.css"],moduleId: module.id
})
export class DistrictComponent implements OnInit {
districtData: ProvinceData;
// tslint:disable-next-line: max-line-length
constructor(private districtservices: DistrictService,private route: ActivatedRoute,private router: RouterExtensions) { }
ngOnInit(): void {
const province = this.route.snapshot.params.province;
this.districtservices.getProvince(province).subscribe(
(data: ProvinceData) => {
this.districtData = data;
console.log(province)
},(error) => console.error(error)
)
}
}
<StackLayout *ngIf="districtData">
<GridLayout android:useDefaultMargins="true" class="test" columns="auto,100" rows="auto,auto" width="2000" height="30" *ngFor="let item of districtData.district">
<Label class="list-province" nsRouterLink ="/district" [text]="item" col="0" row="0"></Label>
<Label class ="arrow-icon" text="" class="fas" col="1" row="0" ></Label>
</GridLayout>
</StackLayout>
解决方法
您需要从角度使用HttpParams
。并在您的http get
通话中传递它。
您的代码将如下所示。
let httpParams = new HttpParams();
httpParams = httpParams.append(province_name,value);
getProvince(province: string){
return this.http.get<ProvinceData>(`${this.url}${province}`,{headers: this.headers,httpParams});
}
注意:使用查询参数时,您无需在网址末尾传递/
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。