0

I tried to change the variable data using axios, i am using vue-axios and vue cli 3.

This is the code:

const qs = require('qs')
export default {
  name: 'Home',
  data: function () {
    return {
      email: null,
      errEmail: false,
      baseUrl: 'https://www.example.com/isemail.php'
    }
  },
  methods: {
    next: function () {
    },
    err: function () {
      this.axios.post(this.baseUrl + 'functions/isEmail.php', qs.stringify({
        value: this.email
      }))
        .then(function (resp) {
          this.errEmail = true
        })
    }
  }
}
<div v-if="errEmail">Target Success</div>

Actually i am trying change the errEmail variable depend on the server callback like this:

this.errEmail = resp.data.isemail

but using constant seems not working too.

1
  • 1
    You need to use an arrow function for the then() callback, or this inside the function will refer to the function itself, not your Vue app context. Commented Jul 7, 2020 at 11:22

1 Answer 1

4

Change this

.then(function (resp) {
  this.errEmail = true
})

to this

.then((resp) => {
  this.errEmail = true
})

Or manually bind this

   .then(function (resp) {
      this.errEmail = true
    }.bind(this))
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.