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

时间:2017-08-18 17:09:11

标签: javascript dojo require amd

我遇到了一些困难,包括我的小部件中来自https://datatables.net/manual/installation#Local-installation的数据表插件。

在jquery.datatables.js中有一个函数可以将jquery分配给变量$

ghci> :set -XOverloadedStrings
ghci> JSON.encode (RoseTree 1 [])
"{\"children\":[],\"value\":1}"
ghci> JSON.encode (RoseTree 1 [RoseTree 2 []])
"{\"children\":[{\"children\":[],\"value\":2}],\"value\":1}"
ghci> JSON.decode "{\"children\":[],\"value\":1}" :: Maybe (RoseTree Int)
Just (RoseTree {value = 1, children = []})
ghci> JSON.decode "{\"children\":[{\"children\":[],\"value\":2}],\"value\":1}" :: Maybe (RoseTree Int)
Just (RoseTree {value = 1, children = [RoseTree {value = 2, children = []}]})

但是,我的警报($)显示$未定义,它应该是JQuery的函数构造函数

在任何情况下,在jquery.datatables.min的后续函数中都有一个主函数:

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 );
    } );

并且在这个函数中,首次使用$是我得到的地方 未捕获的TypeError:$不是函数

为什么我收到此错误,是否由于我对jquery的定义导致此问题?

即使我这样做:

(function( $, window, document, undefined ) 

在主函数之前,我还没有在全局中获得$ .fn.datatables函数?

我知道这个问题有点乱,但我希望有人能指点我从哪里开始研究如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

尝试直接从标题中的CDN加载jquery,而不是尝试使用AMD加载。

确保jquery是第一个要在标题中加载的库,例如:

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