在我的网站上有用户和访客,我希望两者都能在我的网站上“喜欢”元素。对于用户来说,检查他们是否已经“喜欢”了一个特定的元素,以防止他们给同一个元素另一个“喜欢”,这不是问题,但我不确定如何检查访客是否“喜欢”了一个元素,以防止一个特定访客的多次点赞。我正在使用Angular,我想过用cookies保存这些信息,但我还不知道如何保存,以及它是否足够可信。有关于如何实现它们的想法吗?谢谢。
发布于 2020-06-26 16:32:56
最好的方法是使用 localStorage ,更复杂的是在localStorage中使用Hush (值和键)。
HTML
<button type="button" (click)="liked()">
<span *ngIf="!unlike">like</span>
<span *ngIf="unlike">unlike</span>
{{like}}
</button>TS
like=99
unlike:Boolean=true
liked(){
if(localStorage.getItem('like') == null){
localStorage.setItem('like','true')
this.like=this.like+1;
this.unlike =!this.unlike
}else{
localStorage.removeItem('like')
this.like=this.like-1
this.unlike =!this.unlike
}
}like=99;
ipActionLike =['1','2'] ;
unlike:Boolean=true;
liked(){
//get information browser user ip
if(localStorage.getItem('like') == null){
for (var i = 0; i <this.ipActionLike.length; i++) {
if (this.itemArray[i] == ipUser) {
//remove ipUser from array
localStorage.removeItem('like');
this.like=this.like-1;
this.unlike =!this.unlike; }
else{
//push ipUser to array
localStorage.setItem('like','true')
this.like=this.like+1;
this.unlike =!this.unlike }
}
}
}else{
//push ipUser to array
localStorage.removeItem('like')
this.like=this.like-1
this.unlike =!this.unlike
}
}希望这能有所帮助。
https://stackoverflow.com/questions/62532641
复制相似问题