【发布时间】:2020-07-01 06:20:47
【问题描述】:
我正在使用离子段和嵌套 JSON。
现在我正在使用 ionic 使用 Angular 框架。
ionic 版本为 5。 现在我无法在 html 文件中显示。 两个页面都显示在下面。
{{item.tablist.name}} 没有给我没有 2 离子卡的数据。
数据.json
[
{
"title": "TravelTime",
"tablist": [
{
"id": 1,
"name": "TT 1 Average Travel Time",
"current":54,
"historicaldata":56,
"twohr": 27.9,
"yesterday": 28.9,
"week": 30,
"month": 42,
"quater": 24,
"Year": 36,
"percent": 85
},
{
"id": 2,
"name": "TT 2",
"current":54,
"historicaldata":56,
"twohr": 27.9,
"yesterday": 28.9,
"week": 30,
"month": 42,
"quater": 24,
"Year": 36,
"percent": 85
}
]
},
{
"title": "Speed",
"tablist": [
{
"id": 1,
"name": "SS 1 ",
"current":54,
"historicaldata":56,
"twohr": 27.9,
"yesterday": 28.9,
"week": 30,
"month": 42,
"quater": 24,
"Year": 36,
"percent": 85
},
{
"id": 1,
"name": "SS 2 test data",
"current":54,
"historicaldata":56,
"twohr": 27.9,
"yesterday": 28.9,
"week": 30,
"month": 42,
"quater": 24,
"Year": 36,
"percent": 85
}
]
}
]
现在是html文件
<ion-segment color="primary" [(ngModel)]="prit">
<ion-segment-button *ngFor="let item of data" value="{{item.title}}">
{{item.title}}
</ion-segment-button>
</ion-segment>
<div *ngFor="let item of data" [ngSwitch]="prit">
<ion-card *ngFor="let item of data" >
<ion-list *ngSwitchCase="item.title">
<ion-item>{{item.tablist.name}}</ion-item>
</ion-list></ion-card>
</div>
Ts 文件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-test-component',
templateUrl: './test-component.component.html',
styleUrls: ['./test-component.component.scss'],
})
export class TestComponentComponent implements OnInit {
data:any;
prit:string;
constructor() {
this.read_data();
}
read_data(){
fetch('./assets/data.json').then(res => res.json())
.then(json => {
this.data = json;
});
}
ngOnInit(){
this.prit = "1";
}
}
我使用 ionicframework 网站,所有代码都可以正常工作,但 wordking 不是从 json 调用数据。 请帮我解决这个问题
【问题讨论】:
-
请分享您的组件代码。你如何加载 json 等。
-
@Eldar 我也添加了 TS 文件
标签: arrays json angular arraylist ionic5