如何使用本地存储为某物添加一个赞但仅添加一次?
How to add a like to something but only once using local storage?
我正在使用 API,我需要放映电影并点赞它们,但我需要点赞一次。我试图添加一个布尔值,结果是这样的:
let beenLiked = false;
const movieLike = document.getElementById("likeButton")
movieLike.addEventListener('click', () => {
console.log("ok like")
axios.patch(`myUrl`)
beenLiked = true
}, true)
问题是当我喜欢一部电影时,我不能因为我的布尔值而喜欢另一部电影,我该如何修改它来解决我的问题?
如果你能得到应该被点赞的电影的名字(我没有你的 html 文件可以帮你做),你可以跟踪名字并像这样做:
let beenLiked = []
const movieLike = document.getElementById("likeButton")
movieLike.addEventListener('click', () => {
const movie = "The Movie" // Get the movie name or id which has to be liked
if(beenLiked.indexOf(movie) === -1){
console.log("ok like")
axios.patch(`myUrl`)
beenLiked.push(movie)
return true
}
console.log("Already liked once")
return false
})
这里是没有html的简化版:
let beenLiked = []
function a() {
const movie = "The Movie" // Get the movie the likeButton is attached to
if (beenLiked.indexOf(movie) === -1) {
beenLiked.push(movie)
return true
}
return false
}
console.log(a())
console.log(a())
我正在使用 API,我需要放映电影并点赞它们,但我需要点赞一次。我试图添加一个布尔值,结果是这样的:
let beenLiked = false;
const movieLike = document.getElementById("likeButton")
movieLike.addEventListener('click', () => {
console.log("ok like")
axios.patch(`myUrl`)
beenLiked = true
}, true)
问题是当我喜欢一部电影时,我不能因为我的布尔值而喜欢另一部电影,我该如何修改它来解决我的问题?
如果你能得到应该被点赞的电影的名字(我没有你的 html 文件可以帮你做),你可以跟踪名字并像这样做:
let beenLiked = []
const movieLike = document.getElementById("likeButton")
movieLike.addEventListener('click', () => {
const movie = "The Movie" // Get the movie name or id which has to be liked
if(beenLiked.indexOf(movie) === -1){
console.log("ok like")
axios.patch(`myUrl`)
beenLiked.push(movie)
return true
}
console.log("Already liked once")
return false
})
这里是没有html的简化版:
let beenLiked = []
function a() {
const movie = "The Movie" // Get the movie the likeButton is attached to
if (beenLiked.indexOf(movie) === -1) {
beenLiked.push(movie)
return true
}
return false
}
console.log(a())
console.log(a())