0

I am trying to use Jquery in collaboration wit another js framework called imagemapster however i receive this error "TypeError: $ is not a function". However i have imported the jquery module as expected. I imported jquery via npm into my node modules and then referenced it with require js. When i type $ in the console window i receive an undefined so i must be with how i am importing it.

code

<script>
const { $, jQuery } = require('jquery');
global.$ = $;
global.jQuery = jQuery;
//require( '../../../node_modules/jquery-imagemapster/dist/jquery.imagemapster.min.js');

export default {
    name: "Package",
    data: function() {
        return {
            packageImage: "../../../src/assets/images/package.png",
        }
    },
     mounted(){
        $(function() {
            $('#image').mapster({
                mapKey: 'id',
                fillColor: '000000',
                fillOpacity: 0.5,
                areas: 
                [{
                    key: 'firewall',
                    staticState: false
                },
                {
                    key: 'website',
                    staticState: false
                },
                {
                    key: 'server',
                    staticState: false
                },
                {
                    key: 'database',
                    staticState: false,
                    fill: false
                }]
            });
        });
    }
}
4
  • did you try with :" import $ from 'jquery' " ? you could also put "window.$ = require('jquery')" in your app.js Commented Oct 2, 2020 at 9:26
  • i got it to work using this import JQuery from 'jquery'; let $ = JQuery; however when i require the other js framework i receive this error Cannot read property 'defer' of undefined Commented Oct 2, 2020 at 9:29
  • could you remove iife ? Commented Oct 2, 2020 at 12:25
  • 1
    whats global.? search require('jquery') vue, your find about 28m results Commented Oct 2, 2020 at 12:30

1 Answer 1

1

My solution with my above comment and without iife:

<script>
import $ from 'jquery';
//require( '../../../node_modules/jquery-imagemapster/dist/jquery.imagemapster.min.js');

export default {
    name: "Package",
    data: function() {
        return {
            packageImage: "../../../src/assets/images/package.png",
        }
    },
     mounted(){
            $('#image').mapster({
                mapKey: 'id',
                fillColor: '000000',
                fillOpacity: 0.5,
                areas: 
                [{
                    key: 'firewall',
                    staticState: false
                },
                {
                    key: 'website',
                    staticState: false
                },
                {
                    key: 'server',
                    staticState: false
                },
                {
                    key: 'database',
                    staticState: false,
                    fill: false
                }]
            });
        
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.