angular - 如何在不重新加载或刷新 ionic 4 页面的情况下获得类似的帖子状态?
问题描述
我有一个动态的帖子列表。在每篇文章中,我都添加了“赞”和“评论”按钮。我的问题是,当我单击“赞”按钮时,我必须手动重新加载页面才能显示更改。
我想到的一种解决方案是向 like 按钮的(click)
事件添加 API 调用,以便它重新加载页面,但这看起来不太好。
page.html
<ion-col *ngIf="feed.IsLike" tappable>
<ion-icon (click)="toggleLikeState(feed.UserId, feed.PostId);$event.stopPropagation();" tappable
name="heart"></ion-icon>
<span *ngIf="feed.LikesCount > 0">{{feed.LikesCount}}</span>
</ion-col>
<ion-col *ngIf="!feed.IsLike" tappable>
<ion-icon (click)="toggleLikeState(feed.UserId, feed.PostId);$event.stopPropagation();" tappable
name="heart-empty"></ion-icon>
<span *ngIf="feed.LikesCount > 0">{{feed.LikesCount}}</span>
</ion-col>
页面.ts
toggleLikeState(UserId: number, PostId: number) {
this.storage.get('userID').then(async (loggedinUId) => {
const value: {
LoginUserId: string,
UserId: number,
PostId: number
} = {
LoginUserId: loggedinUId,
UserId: UserId,
PostId: PostId
};
this.apiService.postLike(value).then(async (success) => {
console.log("succ", success);
if (success = 0) {
console.log(success);
this.IsLike = !this.IsLike;
this.apiService.getPostsfeeds().then((data: any[]) => {
this.feedlist = data;
});
if (this.IsLike) {
this.iconName = 'heart';
} else {
this.iconName = 'heart-empty';
}
} else {
this.IsLike = !this.IsLike;
this.apiService.getPostsfeeds().then((data: any[]) => {
this.feedlist = data;
});
if (this.IsLike) {
this.iconName = 'heart';
} else {
this.iconName = 'heart-empty';
}
}
}, error => {
console.log("Error", error);
});
});
}
Here my code, is there any way to display like's without reloading the page or I have to user socket.io?
解决方案
好吧,对于初学者来说,您可以通过使用三元运算符而不是 *ngIf 指令来改进 HTML 视图。您只能将其应用于图标的名称,因为其余的标记将保留在这两种情况下。
{{ condition? 'conditionWasTrue' : 'conditionWasFalse' }}
是在一行中编写 if-else 的好方法,那么您的 HTML 代码将如下所示:
<ion-col tappable>
<ion-icon (click)="toggleLikeState(feed.UserId,feed.PostId);$event.stopPropagation();"
tappable name="{{feed.IsLike ? 'heart' : 'heart-empty'}}">
</ion-icon>
<span *ngIf="feed.LikesCount > 0">{{feed.LikesCount}}</span>
</ion-col>
然后在您的 typeScript 文件中,您将不再需要“iconName”变量,因此您可以删除它的大部分内容,它会开始看起来更干净。更好的是,您可以将接口声明移到函数范围之外:
编辑:如果您想在不刷新视图的情况下更新点赞数,则需要在apiService.postLike()
方法响应期间返回新的点赞数。
在 postLike() 成功时更新后端 API 以包含新的 LikesCount 后,您可以使用新response.LikesCount
属性来更新组件的内部变量:
interface UserLikedPost {
LoginUserId: string,
UserId: number,
PostId: number
}
toggleLikeState(UserId: number, PostId: number) {
this.storage.get('userID').then(async (loggedinUId) => {
const value: UserLikedPost = {
LoginUserId: loggedinUId,
UserId: UserId,
PostId: PostId
};
this.apiService.postLike(value).then(
async (success) => {
console.log("succ", success);
this.IsLike = !this.IsLike;
this.feed.LikesCount = success.LikesCount; // Here is where you update your likes without refreshing.
this.apiService.getPostsfeeds().then((data: any[]) => {
this.feedlist = data;
});
}, (error) => {
console.log("Error", error);
});
});
}
每次您想使用新信息更新屏幕视图时,您都需要执行 API 调用,但它不需要看起来那么糟糕。希望这对您有所帮助!
推荐阅读
- r - 使用距离公式调整坐标
- stm32 - STM32 WFI被调用后立即退出
- actions-on-google - 对 Google 采取行动阻止成人关键字
- netlogo - 如何修复意外保存在 NetLogo 3D 中的(2D)NetLogo 模型?
- iphone - 如何将设备系列更改为仅 iPhone?
- javascript - Readmore 选项在某些字符后自动显示
- gnuplot - 使用 gnuplot,如何创建等高线图的 eps 文件?
- java - 错误 SparkContext:初始化 SparkContext 时出错 - Java + Eclipse + Spark
- javascript - 使用地图功能时如何将每个状态动态绑定到每个复选框
- ruby-on-rails - Ruby on Rails ActiveRecord - 使关系错误