简体   繁体   English

如何刷新Ionic页面中的数据?

[英]How to refresh data in a Ionic page?

I'm working on Ionic 4 and I'm trying to understand the basic concepts of it. 我正在研究Ionic 4,并且试图了解它的基本概念。 So I have encountered a problem with the application I'm working currently. 因此,我目前正在使用的应用程序遇到了问题。

One of its function is to display the information of the user, everything shows correctly except two variables. 其功能之一是显示用户信息,除两个变量外,所有内容均正确显示。 This is my part of .ts of the page 这是我的页面.ts的一部分

ngOnInit() {
this.slug = this.route.snapshot.paramMap.get('id');
this.loadData(); }

loadData() {
if (this.dataService.loaded) {
  this.userList = this.dataService.getUserlist(this.slug);
} else {
  this.dataService.load().then(() => {
    this.userList = this.dataService.getUserlist(this.slug);
  });
}}

... ...

displayActvLvl(): string {

let temp: string;
let val: number;
val = this.userList.actLvlVal;
if (val === 1.2) {
  temp = 'No Active';
} else if (val === 1.375) {
  temp = 'Lightly Act';
} else if (val === 1.55) {
  temp = 'Moderate Active';
} else if (val === 1.75) {
  temp = 'Very Active';
} else if (val === 1.9) {
  temp = 'Athlete';
}
return temp;}

displayGender(): string {
if (this.userList.gender) {
  return 'Female';
} else {
  return 'Male';
} }

and this is the two items that are not showing the information 这是两个没有显示信息的项目

    <ion-row>
  <ion-col size="6">
    <ion-item text-center>
      <ion-label position="stacked">{{displayGender()}}</ion-label>
      <ion-note>Gender</ion-note>
    </ion-item>
  </ion-col>

  <ion-col size="6">
    <ion-item text-center>
      <ion-label position="stacked">{{displayActvLvl()}}</ion-label>
      <ion-note>Activity Level</ion-note>
    </ion-item>
  </ion-col>
</ion-row>

I know the issue but i can not figure out a solution. 我知道这个问题,但我找不到解决办法。 For what i understand, when the data is loaded from the Data Service, and then the page is initalize, that's when the two functions run but the two items don't refresh themselves. 据我了解,当从数据服务中加载数据,然后页面被初始化时,这就是两个函数运行但两个项自身不会刷新的时候。 Any help? 有什么帮助吗?

this is the load function from the data Service 这是数据服务的加载功能

load(): Promise<boolean> {
return Promise.resolve(true);

} Edit: GetUserList function from the Data Service 编辑:来自数据服务的GetUserList函数

getUserlist(id): UserInfo { return this.userList.find(userinfo => userinfo.id === id);}

The async pipe is used with Observables or Promises. 异步管道与Observables或Promises一起使用。 Since your functions are synchronous, removing the pipes from the template html should probably fix the issue? 由于您的功能是同步的,因此从模板html中删除管道可能会解决此问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM