0

I'm trying to reuse a method within another of my vue js methods like this :-

sendCode: function () {

            this.showSection("SENDING");


            $.ajax({
                url: "someurl" + app.searchResponse.Id,
                type: "POST",
                contentType: "application/json",

                success: function (result) {

                    if (result.Success) {

                        this.showSection("SMS SENT");
                    }
                    else {
                        this.showSection("SMS FAILED");
                    }

                },
                error: function (error) {

                    console.log(error);
                    this.showSection("SMS FAILED");

                }
            });

        },
        showSection: function (section) {

            return app.ui.currentSection = section;
        }

But i get caught Type Error stating this.showSection() is not a function.

1 Answer 1

1

inside ajax callbacks, vue instance this is not available because it's a different scope. So declare $this = this with a variable before ajax and use $this inside ajax callbacks.

sendCode: function () {

    this.showSection("SENDING");

    var $this = this;

    $.ajax({
        url: "someurl" + app.searchResponse.Id,
        type: "POST",
        contentType: "application/json",

        success: function (result) {

            if (result.Success) {

                $this.showSection("SMS SENT");
            }
            else {
                $this.showSection("SMS FAILED");
            }

        },
        error: function (error) {

            console.log(error);
            $this.showSection("SMS FAILED");

        }
    });

},
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.