Calling method from compiled js file

mdobrenko

I seem to be unable to call a method from a JS package from one of my other files (all.js). I am using Laravel 5.4, VueJS2, and VueRouter. This is an SPA where Laravel serves as a backend.

I get the following error: TypeError: $.LoadingOverlay is not a function

I am utilizing web pack and compiling the assets like this:

mix.js([
    'resources/assets/js/bootstrap.js',
    'resources/assets/js/helpers.js',
    '././node_modules/gasparesganga-jquery-loading-overlay/src/loadingoverlay.min.js',
    'resources/assets/js/app.js',
    '././node_modules/moment/min/moment.min.js',
    '././node_modules/moment-duration-format/lib/moment-duration-format.js',
    ], 'public/js/all.js').version();

The file containing the code I am trying to pull in is the loadingoverlay.min.js file. The file that is referencing it is right underneath it (app.js).

My app.js file:

import Vue from 'vue';
import VueRouter from 'vue-router';

var $ = require('jquery');

Vue.use(VueRouter);

const routes = [
  .. routes here
];


  const router = new VueRouter({
    routes,
    mode: 'history',
  });

  router.beforeEach((to, from, next) => {
    console.log('Entering route');
    $.LoadingOverlay('show');
    next();
  });

  router.afterEach((to, from) => {
    console.log('Entered route');
    $.LoadingOverlay('hide');
  })

 const app = new Vue({
    router
  }).$mount('#app');

The blade file that is responsible for pulling in the compiled assets:

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="author" content="">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <link rel="stylesheet" href="{{ mix('css/all.css') }}">
    <meta name="csrf-token" content="{{ csrf_token() }}"/>
</head>

<body>          
    <div id="app">
        <section class="container">
            <div>
                <router-view></router-view>
            </div>
        </section>
    </div>
    <script src="{{ mix('js/all.js') }}"></script>
</body>
</html>

Interestingly enough, I am able to call the method from that file from my blade page by adding <script></script> tags after the all.js file is pulled in, and calling it from there.

Any ideas on why this may be happening?

Adam

Exactly as it says, because $.LoadingOverlay is not a function.

The jQuery compiled into your all.js via webpack (done via nodejs) will cause the $ = require('jQuery') to be different than the one globally exposed via window.$

window.$ is referencing jQuery 1.11.0 and the one inside your app.js is referencing the jQuery built with webpack.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a method in an angular Component from an external JS file

From Dev

Reading from file then excetuing Js code by calling specific method

From Dev

Calling a method from a helper file

From Dev

Calling a method from another jar file

From Dev

Sinon stub method calling from another file

From Dev

Loading symbols from so file and calling method

From Dev

Calling server method opening file from AJAX

From Dev

Calling a static method from cshtml file

From Dev

AbstractMethodError when calling overridden method of abstract class from a class compiled at runtime

From Dev

Can js file be compiled in a different folder from the source folder?

From Dev

Vue - calling REST API from js file

From Dev

Calling .js file from HTML using AJAX

From Dev

Calling npx from a node.js file

From Dev

Calling Routes from a js angular 1.5.8 file

From Dev

Calling a function from a JS file not working

From Dev

JS - Calling method in class from other method in same class

From Dev

Calling method in PHP file from separate JavaScript file

From Dev

Calling a Javascript function from an external js file in the html file

From Dev

Error when calling a method from separate Python file

From Dev

Calling a method from another file AttributeError: object has no attribute

From Dev

Calling static method from m-file (octave/matlab)

From Dev

Calling non-static method from another file/class

From Dev

Undefined Method - Calling a class in one file from another

From Dev

Vue.js calling an async function from external js file

From Dev

Calling Function from another JS file(not a game state) Phaser js

From Dev

Calling a JS function from a JS-File in PHP

From Dev

Vue.js - Calling a component's method from Vue instance

From Dev

Ractive.js Calling object method from proxy event

From Dev

calling method from one controller to another controller in angular js

Related Related

  1. 1

    Calling a method in an angular Component from an external JS file

  2. 2

    Reading from file then excetuing Js code by calling specific method

  3. 3

    Calling a method from a helper file

  4. 4

    Calling a method from another jar file

  5. 5

    Sinon stub method calling from another file

  6. 6

    Loading symbols from so file and calling method

  7. 7

    Calling server method opening file from AJAX

  8. 8

    Calling a static method from cshtml file

  9. 9

    AbstractMethodError when calling overridden method of abstract class from a class compiled at runtime

  10. 10

    Can js file be compiled in a different folder from the source folder?

  11. 11

    Vue - calling REST API from js file

  12. 12

    Calling .js file from HTML using AJAX

  13. 13

    Calling npx from a node.js file

  14. 14

    Calling Routes from a js angular 1.5.8 file

  15. 15

    Calling a function from a JS file not working

  16. 16

    JS - Calling method in class from other method in same class

  17. 17

    Calling method in PHP file from separate JavaScript file

  18. 18

    Calling a Javascript function from an external js file in the html file

  19. 19

    Error when calling a method from separate Python file

  20. 20

    Calling a method from another file AttributeError: object has no attribute

  21. 21

    Calling static method from m-file (octave/matlab)

  22. 22

    Calling non-static method from another file/class

  23. 23

    Undefined Method - Calling a class in one file from another

  24. 24

    Vue.js calling an async function from external js file

  25. 25

    Calling Function from another JS file(not a game state) Phaser js

  26. 26

    Calling a JS function from a JS-File in PHP

  27. 27

    Vue.js - Calling a component's method from Vue instance

  28. 28

    Ractive.js Calling object method from proxy event

  29. 29

    calling method from one controller to another controller in angular js

HotTag

Archive