angular - Ionic 4-使用 BehaviorSubject 接收变量更新
问题描述
在家里,我有一个产品列表,点击后会打开产品模式,并带有一个添加到购物车的按钮。当您添加金额时,图标旁边会出现。我想在 app.component 中关闭此模式以更新页脚中的图标。我需要使用在未刷新的情况下关闭的模式值更新此购物车。
服务商.ts
public totalShop: BehaviorSubject<String>;
constructor() {
this.totalShop = new BehaviorSubject('0');
}
setTotalShop(val) {
this.totalShop.next(val);
}
clearTotalShop() {
this.totalShop.next('0');
}
getTotalShop(){
return this.totalShop.asObservable();
}
modalProducts.ts
fnShop(){
quantidade = quantidade +1
this.servico.setTotalShop(quantidade)
}
app.component.ts
quantshop;
subscription: Subscription;
ngOnInit(): void {
this.subscription = this.servico.getTotalShop().subscribe(val => this.quantshop = val);
console.log(this.quantshop)
}
应用程序模块
providers: [servico]
图像
解决方案
我想你使用有角度的材料徽章。
您可以在 html 中使用角度异步管道。
<mat-icon [matBadge]="global.getTotalShop() | async">icon</mat-icon>
您需要将服务公开以在 html 中访问它或在您分配 BehaviourSubject 的 component.ts 中创建另一个变量。
推荐阅读
- jquery - 使用空错误数组将 json 数据绑定到数据表
- java - 运行简单的 Servlet 类的问题
- microsoft-graph-api - 使用仅限应用的令牌时,Microsoft Graph 返回“令牌不包含权限,...”
- mysql - MySQL 5.7 和 8.0 的不同行为
- python - Numpy random - 指定多个边界和步骤
- vim - “--更多--”在我使用 :g/pattern 时被禁用
- php - 如何使用 Symfony 原则从我的数据库中克隆所有数据?
- mysql - SQL 查询以说明具有中间名的用户
- python-3.x - 如何在python中修复“找不到此类文件错误”
- microsoft-cognitive - 如何从 Azure 认知服务桌面访问 API 测试控制台?