|
@@ -1,33 +1,41 @@
|
|
import { Component, OnInit } from '@angular/core';
|
|
import { Component, OnInit } from '@angular/core';
|
|
import { Router } from '@angular/router';
|
|
import { Router } from '@angular/router';
|
|
-import { AlertController, IonButton, IonButtons, IonContent, IonHeader, IonIcon, IonInput, IonItem, IonLabel, IonTitle, IonToolbar } from '@ionic/angular/standalone';
|
|
|
|
|
|
+import { AlertController } from '@ionic/angular';
|
|
import { FormsModule } from '@angular/forms'; // 导入 FormsModule
|
|
import { FormsModule } from '@angular/forms'; // 导入 FormsModule
|
|
import { CommonModule } from '@angular/common'; // 导入 CommonModule
|
|
import { CommonModule } from '@angular/common'; // 导入 CommonModule
|
|
-import { chevronBackOutline } from 'ionicons/icons';
|
|
|
|
-import { addIcons } from 'ionicons';
|
|
|
|
-addIcons({chevronBackOutline});
|
|
|
|
|
|
+import { IonicModule } from '@ionic/angular';
|
|
|
|
+
|
|
@Component({
|
|
@Component({
|
|
selector: 'app-page-bmi',
|
|
selector: 'app-page-bmi',
|
|
templateUrl: './page-bmi.component.html',
|
|
templateUrl: './page-bmi.component.html',
|
|
styleUrls: ['./page-bmi.component.scss'],
|
|
styleUrls: ['./page-bmi.component.scss'],
|
|
standalone: true,
|
|
standalone: true,
|
|
- imports:[IonHeader,IonToolbar,IonTitle,IonButton,IonButtons,IonContent,IonItem,IonLabel,IonInput,FormsModule,CommonModule,IonIcon]
|
|
|
|
|
|
+ imports: [FormsModule, CommonModule, IonicModule], // 导入相关模块
|
|
})
|
|
})
|
|
-export class PageBmiComponent implements OnInit {
|
|
|
|
|
|
+export class PageBmiComponent implements OnInit {
|
|
|
|
|
|
- ngOnInit() {this.resetForm();} // 页面加载时清空数据
|
|
|
|
- height: number|null =null; // 身高
|
|
|
|
- weight: number|null =null; // 体重
|
|
|
|
- bmi: number|null=null; // BMI 值
|
|
|
|
|
|
+ height: number | null = null; // 身高
|
|
|
|
+ weight: number | null = null; // 体重
|
|
|
|
+ bmi: number | null = null; // BMI 值
|
|
|
|
+ bmiStatus: string | null = null; // BMI 状态提示
|
|
|
|
|
|
- constructor(private router: Router,private alertController: AlertController) {this.resetForm();}
|
|
|
|
|
|
+ constructor(private router: Router, private alertController: AlertController) {
|
|
|
|
+ this.resetForm(); // 初始化时清空数据
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ngOnInit() {
|
|
|
|
+ this.resetForm(); // 页面加载时清空数据
|
|
|
|
+ }
|
|
|
|
|
|
// 计算 BMI
|
|
// 计算 BMI
|
|
async calculateBMI() {
|
|
async calculateBMI() {
|
|
- if (this.height !=null && this.weight !=null) {
|
|
|
|
- if(this.height >0 && this.weight >0) {
|
|
|
|
- const heightInMeters = this.height / 100; // 将身高转换为米
|
|
|
|
- this.bmi = this.weight / (heightInMeters * heightInMeters); // 计算 BMI
|
|
|
|
|
|
+ if (this.height != null && this.weight != null) {
|
|
|
|
+ if (this.height > 0 && this.weight > 0) {
|
|
|
|
+ const heightInMeters = this.height / 100; // 将身高转换为米
|
|
|
|
+ this.bmi = this.weight / (heightInMeters * heightInMeters); // 计算 BMI
|
|
|
|
+
|
|
|
|
+ // 检查 BMI 并显示相应的提示
|
|
|
|
+ this.checkBmiStatus(this.bmi);
|
|
} else {
|
|
} else {
|
|
const alert = await this.alertController.create({
|
|
const alert = await this.alertController.create({
|
|
header: '身高体重不能为0!',
|
|
header: '身高体重不能为0!',
|
|
@@ -41,21 +49,32 @@ export class PageBmiComponent implements OnInit {
|
|
buttons: ['确定']
|
|
buttons: ['确定']
|
|
});
|
|
});
|
|
await alert.present(); // 显示弹出框
|
|
await alert.present(); // 显示弹出框
|
|
-
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- //清空数据
|
|
|
|
- // 清空输入数据
|
|
|
|
- resetForm() {
|
|
|
|
- this.height = null;
|
|
|
|
- this.weight = null;
|
|
|
|
- this.bmi = null;
|
|
|
|
|
|
+ // 检查 BMI 并给出分类提示
|
|
|
|
+ checkBmiStatus(bmi: number) {
|
|
|
|
+ if (bmi < 18.5) {
|
|
|
|
+ this.bmiStatus = '低体重:BMI 小于 18.5,建议增加体重';
|
|
|
|
+ } else if (bmi >= 18.5 && bmi < 24.9) {
|
|
|
|
+ this.bmiStatus = '正常体重:BMI 在 18.5 到 24.9 之间,保持健康';
|
|
|
|
+ } else if (bmi >= 25 && bmi < 29.9) {
|
|
|
|
+ this.bmiStatus = '超重:BMI 在 25 到 29.9 之间,建议控制体重';
|
|
|
|
+ } else {
|
|
|
|
+ this.bmiStatus = '肥胖:BMI 大于或等于 30,建议减肥';
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 清空数据
|
|
|
|
+ resetForm() {
|
|
|
|
+ this.height = null;
|
|
|
|
+ this.weight = null;
|
|
|
|
+ this.bmi = null;
|
|
|
|
+ this.bmiStatus = null; // 清空 BMI 状态提示
|
|
|
|
+ }
|
|
|
|
|
|
// 返回上一个页面
|
|
// 返回上一个页面
|
|
goBack() {
|
|
goBack() {
|
|
this.router.navigate(['/tabs/tab2'], { replaceUrl: true }); // 使用 replaceUrl 强制重载页面
|
|
this.router.navigate(['/tabs/tab2'], { replaceUrl: true }); // 使用 replaceUrl 强制重载页面
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|