1

I’m using vue with webpack. This is my code:

export default {
data() {
        return {
            start: true,
            logReg: false,
        }
},
methods: {
    checkLogged() {
        this.start = false;
        this.logReg = true;

    },
    closem(pathTo) {
        $('#mm').modal('hide')
        this.$router.push({ path: pathTo })

    }
},
mounted() {


    $('#mm').on('hide.bs.modal', function () {


       this.checkLogged();




    });
},
}

I’m trying to call “checkLogged” method inside the jquery event but it says the function is undefined. How can i call the method?

3
  • Just a note here. You should not mix Jquery with any framework. I'm sure there is a way to acheive what you are trying to do directly in vue. Commented May 15, 2020 at 11:39
  • Explain please.. Commented May 15, 2020 at 12:49
  • Well jquery is a very intrusive "framework" and most of the time it will interfere with the framework you are trying to use. It's better to only use the function that are supported by that framework to do what you are trying to do. This goes for any JS frameworks. Commented May 15, 2020 at 13:41

1 Answer 1

4

This is because the function() {...} creates a new scope, therefore the this inside the callback does not actually refer to the VueJS component anymore, but to the window object instead.

You can use an arrow function, which does not create its own this binding, but passes in the lexical this (i.e. the this in an arrow function is the same this as the context it is called in):

$('#mm').on('hide.bs.modal', () => {
   this.checkLogged();
});
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.