jquerymmenu

How can I destroy the mmenu plugin


I have a menu which is made using the Mmenu plugin. I initialize it at resolutions less than 1025px. I initialize inside resize. I need to destroy mmenu on permissions greater than 1025 so that the usual desktop menu is displayed. Now my problem is that I have an error in the console - Cannot read properties of undefined (reading 'destroy'). How can I solve this problem?

$(window).resize(function(e) {
  if ($(window).width() < 1025) {

    $menu.mmenu({
      "offCanvas": {
        "position": "left"
      },
      "navbar": {
        "title": ""
      },
      "theme": "light"
    });

    const api = $menu.data("mmenu");

  } else if ($(window).width() > 1025) {
    const api = $menu.data("mmenu");
    api.destroy();
  }
});


Solution

  • This could be because, when you try to access a property (destroy()), it has undefined in it and hasn't been assigned a value. It is possible that destroy() is an arrow function and that a value or function is to be assigned later.