【问题标题】:“$.get is not a function” using jQuery from babel-node“$.get 不是函数”使用来自 babel-node 的 jQuery
【发布时间】:2019-01-20 10:19:54
【问题描述】:

我正在使用命令行中的 babel-node 从 test.js 文件中运行以下代码:babel-node test.js

安装 jquery

npm i --save jquery

test.js:

import $ from 'jquery';

$.get("www.google.com");

我收到TypeError: _jquery2.default.get is not a function

有什么建议吗?

请注意,这个问题不是 How to import jquery using ES6 syntax? 的重复问题,因为这似乎是 babel-node 特有的问题(如果我使用带有 babel 的 webpack 创建一个包并从浏览器运行它,则相同的代码可以工作)

【问题讨论】:

    标签: javascript jquery command-line ecmascript-6 babeljs


    【解决方案1】:

    要让它与 NodeJS 一起工作,jQuery 必须在 dom 上下文中执行。如果您需要发出 ajax 请求,像 domJS 这样的模块可以完成这项工作。最好的选择是使用其他特定模块,如“https”或“request”,如下例所示:

    import https from 'https';
    https.get("https://www.google.com");
    

    【讨论】:

      【解决方案2】:

      你的导入应该是import * as $ from 'jquery';

      也很可能是$ 的冲突。尝试添加

       var jq = $.noConflict();
       jq.get("www.google.com");
      

      希望这会有所帮助!

      【讨论】:

      • 我不断收到 TypeError: $.get is not a function
      • 很可能是冲突问题,在脚本标签中添加$.noConflict();,在此处阅读更多内容api.jquery.com/jquery.noconflict
      • 不幸的是我得到 TypeError: _jquery2.default.noConflict is not a function
      猜你喜欢
      • 2019-01-20
      • 1970-01-01
      • 2023-04-07
      • 2016-03-16
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多