2

Below is the set of external javascript functions that I'm calling from a vue instance

// debounce
function debounce(func, wait, immediate) {
    let timeout;
    
    return function() {
        let context = this, args = arguments;
        later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
    
        let callNow = immediate && !timeout;

        clearTimeout(timeout);

        timeout = setTimeout(later, wait);

        if (callNow) func.apply(context, args);
    };
}
// -- end debounce

// animate css
function animateCss(element, animationName, callback) {
    const node = document.querySelector(element)
    node.classList.add('animated', animationName)
    
    function handleAnimationEnd() {
        node.classList.remove('animated', animationName)
        node.removeEventListener('animationend', handleAnimationEnd)
    
        if (typeof callback === 'function') callback()
    }
    
    node.addEventListener('animationend', handleAnimationEnd);
}
// -- end animate css

and my vue instance

new Vue({
    el: '#app',
    template: '#search-tpl',
    methods: {
        onKeyDown: debounce(function(){
            animateCss('#searchboxui-wrapper', 'fadeOutDown',function(){
                document.querySelector('#searchboxui-wrapper').style.display = 'none';  
            });
        }
    }
})

but it always throws me undefined, even if I declare the axios package or socketio package, it throws me undefined, any help, ideas please?

PS: I'm using Vue CLI 3

enter image description here

12
  • 1
    How do you import the things you wanna use in Search.vue ? Commented Feb 14, 2019 at 13:41
  • 1
    Are you using webpack or any other build process, or just including vue.js directly into a .html page? (If you're using .vue files you just need to import whatever external libraries each component is using, so webpack knows to include them). Can you show the code where you're currently trying to "declare the axios package"? Commented Feb 14, 2019 at 13:43
  • @DanielBeck webpack, and im on vue cli Commented Feb 14, 2019 at 15:27
  • 3
    Ah, that's probably the problem. You need to declare the import in the vue file where you're using the imported code, not in app.vue. Commented Feb 14, 2019 at 15:32
  • 1
    Does your external file export the functions? @niklaz's answer below shows exactly how this should work. If that doesn't seem to fix it, please show the import / export code in the question, it's hard to debug based on a description. Commented Feb 14, 2019 at 15:49

1 Answer 1

2

Create external js file, say externals.js, use import to import everything or specific functions from this file and use them in your Vue code:

Content in externals.js:

// debounce
function debounce(func, wait, immediate) {
    let timeout;

    return function() {
        let context = this, args = arguments;
        later = function() {
            timeout = null;

            if (!immediate) func.apply(context, args);
        };
        
        let callNow = immediate && !timeout;

        clearTimeout(timeout);

        timeout = setTimeout(later, wait);

        if (callNow) func.apply(context, args);
    };
}
// -- end debounce
        
// animate css
function animateCss(element, animationName, callback) {
    const node = document.querySelector(element)
    node.classList.add('animated', animationName)
        
    function handleAnimationEnd() {
        node.classList.remove('animated', animationName)
        node.removeEventListener('animationend', handleAnimationEnd)
        
        if (typeof callback === 'function') callback()
    }
        
    node.addEventListener('animationend', handleAnimationEnd);
}
// -- end animate css

export {debounce, animateCss}

Content in VueJS:

import {debounce, animateCss} from '../../js/externals.js'
    
new Vue({
    el: '#app',
    template: '#search-tpl',
    methods: {
        onKeyDown() {
            debounce(function() {
                animateCss('#searchboxui-wrapper', 'fadeOutDown', function() {
                    document.querySelector('#searchboxui-wrapper').style.display = 'none';
                });
            });
        }
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

@JuliverGalleto, can you update the question with corrected sample? It should be working. If it is not working, then something else is the cause of error

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.