首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VueJS axios只允许单击按钮一次,第二次允许在收到数据后单击按钮。

VueJS axios只允许单击按钮一次,第二次允许在收到数据后单击按钮。
EN

Stack Overflow用户
提问于 2021-11-08 15:44:33
回答 3查看 542关注 0票数 0

我喜欢/不喜欢拉拉维尔/VueJS的系统。

我的系统正常工作,但我想避免垃圾邮件发送者。

就像纽扣:

代码语言:javascript
复制
<a v-on:click="like(10, $event)">
    <i class="fas fa-heart"></i>
</a>

10是柱状id,它是在拉拉叶片中生成的。

为了避免垃圾邮件发送者,我试图这样做:

代码语言:javascript
复制
const app = new Vue({
el: '#app',
data() {
    return {
        allowed: true,
    };
},
methods: {
    like: function (id, event) {
        if (this.allowed) {
            axios.post('/posts/' + id + '/like', {  
                post_id: id,
            })
            .then((response) => {
                this.allowed = false; //Set allowed to false, to avoid spammers.

                ..... code which changes fa-heart, changes class names, texts etc ....

                // send notification to user
                Vue.toasted.show('Bla bla bla successfuly liked post', {
                    duration: 2000,
                    onComplete: (function () {
                        this.allowed = true //After notification ended, user gets permission to like/dislike again.
                    })
                });

但这里缺少了一些东西,或者我做错了什么。当我非常快地点击喜欢的图标,并检查请求,axios发送3-4-5请求(取决于你点击的速度)

只有在3到5个请求之后,data.allowed才变成false。为什么?我想:

ended";

  • Previous
  1. =true;
  2. 用户单击如->
  3. = false;>单击第二次“您不允许再次单击,因为以前的通知(而不是

通知)结束了->允许=真;

  1. .循环
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-11-08 16:02:04

代码语言:javascript
复制
    like: function (id, event) {
        // first, check if the `like` can be sent to server
        if (!this.allowed) return;
        // remember that we are sending request, not allowed to `like` again
        this.allowed = false;

        var self = this;  // you need this to remember real this
        axios.post('/posts/' + id + '/like', {  
            post_id: id,
        }).then((response) => {
            ..... code ....

            // send notification to user
            Vue.toasted.show('Bla bla bla successfuly liked post', {
                duration: 2000,
                onComplete: function () {
                    //After notification ended, user gets permission to like/dislike again.
                    self.allowed = true;
                }
            );
       }).catch(function() {
            // maybe you also need this catch, in case network error occurs
            self.allowed = true;
       })
        ....
票数 1
EN

Stack Overflow用户

发布于 2021-11-08 15:49:30

this.allowed = false;正在被调用,直到API调用完成,这样您就可以在这段时间内发送更多垃圾邮件。在false if (this.allowed)之后立即将其设置为if (this.allowed)

代码语言:javascript
复制
if (this.allowed) {
    this.allowed = false; // Then do the call
}
票数 1
EN

Stack Overflow用户

发布于 2021-11-08 15:57:13

对我来说,@click.once

代码语言:javascript
复制
<q-btn
@click.once ="selectItemFiles(options)"
/>

无论用户单击多少次,该操作将只生成一次。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69886206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档