实现商品列表的响应式卡片视图,当屏幕宽度小于768px时,将表格视图切换为卡片视图。
@HostListener 监听窗口大小变化checkScreenSize() 方法检测屏幕宽度MOBILE_BREAKPOINT)isMobileView 状态变量在 HTML 模板中添加了完整的卡片视图结构:
*ngIf="!isMobileView" 控制表格视图显示*ngIf="isMobileView" 控制卡片视图显示添加了完整的卡片样式:
// 添加 HostListener 导入
import { Component, OnInit, HostListener } from '@angular/core';
// 添加响应式状态
isMobileView = false;
private readonly MOBILE_BREAKPOINT = 768;
// 添加屏幕检测方法
@HostListener('window:resize', ['$event'])
onResize(event?: Event): void {
this.checkScreenSize();
}
private checkScreenSize(): void {
this.isMobileView = window.innerWidth < this.MOBILE_BREAKPOINT;
}
// 在 ngOnInit 中调用
ngOnInit(): void {
this.checkScreenSize();
this.loadProducts();
}
*ngIf="!isMobileView" 条件*ngIf="isMobileView"添加了 .card-container 及其子元素的完整样式:
✅ 编译成功,无错误
✅ WHEN screen width is less than 768px, THE Merchant Portal SHALL convert data tables to card list view
实现内容:
src/app/pages/products/product-list/product-list.component.ts
HostListener 导入src/app/pages/products/product-list/product-list.component.html
src/app/pages/products/product-list/product-list.component.scss
成功实现了商品列表的响应式卡片视图功能。当屏幕宽度小于768px时,系统会自动将表格视图切换为更适合移动设备的卡片视图。卡片视图保留了所有必要的商品信息和操作功能,提供了良好的移动端用户体验。
任务完成时间:2025-12-12