简体   繁体   English

未捕获的TypeError:$不是函数(requirejs定义方法)

[英]Uncaught TypeError: $ is not a function ( requirejs define method)

I'm having some difficulty including the datatables plugin from https://datatables.net/manual/installation#Local-installation in my widget. 我遇到了一些困难,包括我的小部件中来自https://datatables.net/manual/installation#Local-installation的datatables插件。

In the jquery.datatables.js there is a function that is supposed to assign jquery to the variable $ 在jquery.datatables.js中,有一个函数应该将jquery分配给变量$

if ( typeof define === 'function' && define.amd ) {
        // AMD
        alert('AMD');
        define( ['assets/js/jquery/jquery-3.2.1'], function ( $ ) {
        alert('Common1');
        alert( $ );
        return factory( $, window, document );
    } );

However, my alert ( $ ) shows that $ is undefined where it's supposed to be the function constructor for JQuery 但是,我的警报($)显示$在应该作为JQuery的函数构造函数的位置未定义

In any case, in the subsequent function of jquery.datatables.min there is a main function that takes in: 无论如何,在jquery.datatables.min的后续函数中,都有一个主要函数可以接受:

(function( $, window, document, undefined ) 

and in this function, on the first use of $ is where I get Uncaught TypeError: $ is not a function 在此函数中,第一次使用$时会出现Uncaught TypeError:$不是函数

Why am I getting this error and is it due to my define of the jquery causing this issue? 为什么会出现此错误,这是由于我对jquery的定义导致了此问题?

Even if I do: 即使我这样做:

var $ = require define( ['assets/js/jquery/jquery-3.2.1'])

right before the main function, I still don't get the $.fn.datatables function in the global? 就在main函数之前,我仍然没有全局获得$ .fn.datatables函数吗?

I know this question is kind of a mess but I was hoping someone could give me pointers of where to start looking on how to resolve this issue. 我知道这个问题有点混乱,但我希望有人能给我一些指导,让他们从哪里开始寻找如何解决此问题的方法。

Try to load jquery directly from CDN in the header and to not try to load using AMD. 尝试直接从标头中的CDN加载jquery,而不尝试使用AMD加载。

Make sure jquery is the first library to be loaded in the header, example: 确保jquery是要在标头中加载的第一个库,例如:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 <!-- other scripts below -->
</head>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM