我目前正在写博客,我有点卡住了,我需要一些帮助!
当用户创建一篇文章时,他可以设置一个特殊的标签,通过属性"position“将文章推送到网站顶部。因此,根据用户的设置,article.position可以等于"first“、"second”、"third“或"none”(这是默认位置)。
下面是使用axios创建文章的方法:
addArticle() {
axios.post('http://localhost:4000/articles', {
title: this.title,
description: this.description,
content: this.content,
position: this.position,
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
},一切都运行得很完美,但问题是:
当用户创建具有特殊位置的文章时,如果文章已经设置到相同的位置,我希望将其替换为用户刚刚设置的新文章。
例如:一个旧的冠词A有position == "first"。用户正在创建一个新的文章B,他希望文章B在顶部,他设置了position == "first"。A条现在应将职位设置为“无”。
重要提示:文章位于Vuex商店。
下面是我在axios请求上面的addArticle方法中尝试的内容:
if (article.position == "first") {
let articlesList = this.$store.state.articles
for (var i = 0; articlesList.length; i++) {
if(articlesList[i].position == "first")
articlesList[i].position == "none"
}
}我是一个新手,所以,它可能有很多错误,我做错了什么?
谢谢您抽时间见我!
发布于 2018-11-01 19:05:44
if (article.position === 'first') {
let articlesList = this.$store.state.articles;
for (i = 0; i < articlesList.length; i++) {
if(articlesList[i].position === 'first')
articlesList[i].position = "none"
}
}我只是做了一些最小的改动,而没有提到最佳实践和其他东西。
https://stackoverflow.com/questions/53099694
复制相似问题