0

I'm trying to implement something to close all menus/modals when a user clicks outside of the menu/modal area.

this.closeMenu() is being invoked correctly but I'm getting an error that says this.closeMenu is not a function. What's the reason for this?

methods: Object.assign({},
  {
    closeMenu(){
      console.log("close menu")
    }
  }
)


mounted(){
  $(document).on('click', function(event) {
    if (!$(event.target).closest('.menu').length){
      // close all menus
      this.closeMenu()
    }
  });
}
jj008
  • 713
  • 11
  • 33

1 Answers1

1

this keyword here is not Vue instance. You can do a trick by assign var self = this

mounted(){
  var self = this;
  $(document).on('click', function(event) {
    if (!$(event.target).closest('.menu').length){
      // close all menus
      self.closeMenu()
    }
  });
}
ittus
  • 17,463
  • 5
  • 38
  • 47