A
A
Abdul Amenov2019-11-29 06:30:43
JavaScript
Abdul Amenov, 2019-11-29 06:30:43

How do I write regular DOM functions in Nuxt.js???

I don’t know how to do it anymore :( I
wrote the function for opening the context menu:

let el = document.querySelector("[data-context-menu]");

  el.onclick = function(e) {
    let target = e.target;
    let id = target.dataset.contextMenuId;
    let activeClass = "context-menu--active";

    console.log([target, id, activeClass]);

    let contextMenu = document.getElementById(id);

    if (contextMenu.classList.contains(activeClass)) {
      return contextMenu.classList.remove(activeClass);
    }

    contextMenu.classList.add(activeClass);
  };

I reload the page and it works every other time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Pospeliri, 2019-12-03
@apospeliri

Why are you using 'plain' JS if you have Nuxt ?
document.querySelector -> to select an element use ref="superPuperMenu" ( https://en.vuejs.org/v2/api/#ref)
el.onclick -> @click="toggleMenu " ( https://en.vuejs.org/v2/api/#ref) vuejs.org/v2/api/#v-on)
add, remove class -> v-if="showMenu" ( https://ru.vuejs.org/v2/guide/conditional.html#v-if)
For beautiful display of the menu, wrap the body of the menu in a transition and add an animation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question