angular - 从属性列表转到属性详细信息页面以显示所选属性的详细信息 404 未找到
问题描述
我正在使用httpClient
从本地json
文件中检索数据。一切都在propertylist
. 当我单击一个属性时,我想转到一个新页面,并显示所选属性的详细信息。我看过无数的视频,阅读了很多指南,但我似乎无法理解。
我相信它与路由、激活的路由和参数有关。我已经创建了页面、服务、导入了router
andactivatedrouter
并注入了我的服务,并:id
在路由模块中设置了路径。
JSON的开始
{
"property": [
{
"propertyID": "101552000007",
"branchID": "1",
"clientName": "Demo",
"branchName": "Br",
"department": "Sales",
"referenceNumber": "10006",
"addressName": "",
"addressNumber": "87",
"addressStreet": "Hackney Road",
"address2": "",
"address3": "London",
"address4": "",
"addressPostcode": "E2 8PP",
"country": "United Kingdom",
}
]
待售页面
<ion-col *ngFor="let property of propertyList.property; let i=index" size="6">
<ion-card [routerLink]="['/read-post',property.propertyID]">
服务
getProperty(propertyID): Observable<IProperties[]> {
return this.http.get<IProperties[]>('/assets/data/xx.json/' + propertyID);
}
阅读后页面
ngOnInit() {
let propertyID = this.route.snapshot.params['id'];
this.saleService.getProperty(propertyID).subscribe((property) => {
console.log(property);
});
}
应用路由
{ path: 'read-post/:id', loadChildren: './read-post/read-post.module#ReadPostPageModule' },
当我在待售页面中单击一个属性时,它会导航到“ http://localhost:8100/read-post/101552000015 ”(这是 json 中的 propertyID)
控制台日志
GET http://localhost:8100/assets/data/xx.json/101552000015 404 (Not Found)
解决方案
您将参数传递给json
不正确的文件,而是读取json
文件,然后过滤数据以获取所需的属性。
根据您在问题中发布的 JSON 数据检查以下代码。
变化getProperty()
及ngOnInit()
方法如下:
getProperty(): Observable<IProperties[]> {
return this.http.get<IProperties[]>('/assets/data/xx.json/');
}
ngOnInit() {
let propertyID = this.route.snapshot.params['id'];
this.saleService.getProperty().subscribe( data => {
if(Boolean(data) && Boolean(data.property)){
let requiredValue = data.property.filter(ele => ele.propertyID === propertyID)[0];
console.log(requiredValue);
}
});
}
推荐阅读
- java - 如何在 CompletableFuture.allOf 之后调用无参数函数
- visual-studio - Visual Studio 2017 图形调试器 - 不支持此类接口
- php - WordPress 将电子邮件内容作为 .HTML 附件发送
- gitlab-ci - gitlab-runner,在创建模式下卡住作业(500 内部服务器错误)
- css - CSS相邻选择器嵌套
- google-cloud-dataflow - 排序不同 Pcollections 输出的写入
- python - 在多元线性回归的特殊情况下增加训练MSE
- android - 绑定 LiveData
到文本视图 - python - 注册 OpenAI Gym 畸形环境失败
- searchkick - 如何为 Elastic Search - FATAL -- 指定搜索字段:[...] ArgumentError(必须指定要搜索的字段):