如何在不重新加载或刷新 ionic 4 页面的情况下获得喜欢的帖子状态?
How to get like posts status without reload or refresh page in ionic 4?
我有一个 post 的动态列表。在每个 post 中,我添加了一个 "like" 和一个 "comment" 按钮。我的问题是,当我单击“赞”按钮时,我必须手动重新加载页面才能显示更改。
我想到的一个解决方案是向点赞按钮的 (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>
page.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" 变量,所以你可以删除它的大部分,它会开始看起来更干净。更好的是,您可以将接口声明移到函数范围之外:
编辑:如果你想在不刷新视图的情况下更新点赞数,你需要在你的回复期间return新的点赞数apiService.postLike()
方法。
更新后端 API 以在 postLike() 成功时包含新的 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 调用,但它不需要看起来那么糟糕。希望对您有所帮助!
我有一个 post 的动态列表。在每个 post 中,我添加了一个 "like" 和一个 "comment" 按钮。我的问题是,当我单击“赞”按钮时,我必须手动重新加载页面才能显示更改。
我想到的一个解决方案是向点赞按钮的 (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>
page.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" 变量,所以你可以删除它的大部分,它会开始看起来更干净。更好的是,您可以将接口声明移到函数范围之外:
编辑:如果你想在不刷新视图的情况下更新点赞数,你需要在你的回复期间return新的点赞数apiService.postLike()
方法。
更新后端 API 以在 postLike() 成功时包含新的 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 调用,但它不需要看起来那么糟糕。希望对您有所帮助!