meal-search.component.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { CommonModule } from '@angular/common';
  2. import { Component, Input, OnInit } from '@angular/core';
  3. import { IonButton, IonButtons, IonCard, IonCardContent, IonCardHeader, IonCardSubtitle, IonCardTitle, IonContent, IonHeader, IonTitle, IonToolbar } from '@ionic/angular/standalone';
  4. import { ModalController } from '@ionic/angular/standalone';
  5. import { MealService } from 'src/app/meal/meal.service';
  6. @Component({
  7. selector: 'app-meal-search',
  8. templateUrl: './meal-search.component.html',
  9. styleUrls: ['./meal-search.component.scss'],
  10. standalone:true,
  11. imports: [IonHeader,IonToolbar,IonContent,IonTitle,IonButton,IonButtons,IonCard,IonCardTitle,IonCardHeader,IonCardContent,CommonModule,IonCardSubtitle]
  12. })
  13. export class MealSearchComponent implements OnInit {
  14. @Input() searchQuery: string = ''; // 从父组件接收搜索词
  15. meals: any[] = []; // 存储查询到的菜谱
  16. constructor(
  17. private mealService: MealService,
  18. private modalController: ModalController
  19. ) {}
  20. ngOnInit() {
  21. // 初始化时调用 searchMeals() 进行查询
  22. if (this.searchQuery.trim()) {
  23. this.searchMeals();
  24. }
  25. }
  26. // 根据搜索查询获取菜品
  27. searchMeals() {
  28. this.mealService.searchMealsByName(this.searchQuery).subscribe(
  29. (response) => {
  30. this.meals = response.meals || []; // 将查询结果赋值给 meals
  31. },
  32. (error) => {
  33. console.error('查询失败', error);
  34. this.meals = []; // 如果查询失败,则将 meals 置为空
  35. }
  36. );
  37. }
  38. // 关闭弹窗
  39. closeModal() {
  40. this.modalController.dismiss();
  41. }
  42. // 点击菜品查看详细信息
  43. }