I
I
IIITRIX2018-09-13 16:12:45
JavaScript
IIITRIX, 2018-09-13 16:12:45

How to include jquery in vue component?

My component

<template>
  <div>Шапка</div>
</template>
<script>
import cookies from 'js-cookie'
export default {
  name: 'navigation',
  props: ['backend'],
  data() {
    return {
      isLogin: !!cookies.get('user')
    }
  },
  methods: {
    login() {
      this.$store.commit('global/showLoginModal', true)
    },
    search(e) {
      var qs = e.target.value
      if (qs === '') {
        return false
      }
      this.$router.replace('/search/' + qs)
    }
  }
}
</script>

Need to add to it
var searchBar = $(".search"),
  searchInput = searchBar.find("input"),
  searchToggleBtn = $(".search-toggle"),
  searchParent = $("header_content");
searchInput.on("focus", function() {
  searchBar.addClass("expanded")
}), searchInput.on("blur", function() {
  $(this).val() || searchBar.removeClass("expanded")
}), searchToggleBtn.on("click", function() {
  $(this).toggleClass("active"), searchParent.toggleClass("search"), searchBar.toggleClass("visible"), searchInput.focus()
});

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Viktor Evgeniev, 2018-09-13
@IIITRIX

Pull it up with a lifecycle hook mounted().
Article about it

A
Alexander Pushkarev, 2018-09-13
@AXP-dev

You don't need jQuery to pull so much unnecessary code.
Seems like it should work

let searchBar       = document.querySelector('.search'),
    searchInput     = searchBar.querySelector('input'),
    searchToggleBtn = document.querySelector('.search-toggle'),
    searchParent    = document.querySelector('.header_content');

searchInput.addEventListener('focus', function (event) {
    searchBar.classList.add('expanded');
});
searchInput.addEventListener('blur', function (event) {
    searchInput.value || searchBar.classList.remove('expanded');
});
searchToggleBtn.addEventListener('click', function (event) {
    searchToggleBtn.classList.toggle('active');
    searchParent.classList.toggle('search');
    searchBar.classList.toggle('visible');
    searchInput.focus();
});

V
Vladimir Malkov, 2018-09-13
@MalkovVladimir73

As an option, like this:

import JQuery from 'jquery'
let $ = JQuery

In general, this is quite redundant, to drag jquery as well. I advise you to rewrite the code on ecma, it will even be the same size, and it will all work much faster.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question