【发布时间】:2023-02-23 16:33:17
【问题描述】:
我需要帮助理解和在 Angular + Ionic 6 应用程序中实现 toast 消息......
在我的应用程序中,我想在清除购物车、提交订单等某些事件时显示吐司消息......在这种情况下,我希望吐司消息显示通过 API 调用发送给我的消息。
我已经尝试过 ionic docs 实现,但我不确定如何调用 toast 消息并将消息传递给它。
在 POSTMAN 中,消息响应如下所示:
{
"message": "You have successfully cleared the cart"
}
这是清除购物车的 API 调用 (cart.service.ts):
clearCart() {
return from(Preferences.get({key: 'TOKEN_KEY'})).pipe(
switchMap(token => {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token.value}`);
return this.httpClient.delete<ShoppingCart>(`${environment.apiUrl}cart`, {headers, observe: 'response'});
}),
catchError(err => {
console.log(err.status);
if (err.status === 400) {
console.log(err.error.message);
}
if (err.status === 401) {
this.authService.logout();
this.router.navigateByUrl('/login', {replaceUrl: true});
}
return EMPTY;
}),
);
}
这是我的购物车页面 (cart.page.ts) 中离子文档中的 clearCart 函数和 presentToast 函数:
clearCart() {
this.cartService.clearCart().subscribe(
(data: any) => {
this.products = [];
this.totalProducts = 0;
this.totalCartPrice = 0;
},
error => {
console.log('Error', error);
});
}
async presentToast(position: 'bottom') {
const toast = await this.toastController.create({
message: 'Hello World!',
duration: 1500,
position
});
await toast.present();
}
我什至在实施吐司消息方面走上了正确的道路,还是一开始就搞砸了? :)
在哪里调用 presentToast 函数?我如何传递其中的消息?我需要制作一个新的 toast 组件吗?
【问题讨论】:
标签: javascript angular typescript ionic-framework