【问题标题】:How to import jQuery from a file using es6如何使用 es6 从文件中导入 jQuery
【发布时间】:2018-07-17 10:29:50
【问题描述】:

由于这个项目的特殊性,我需要使用不在 node_modules 文件夹中的直接路径将jQuery 导入我的main.js,但我无法做到。我发现的所有问题都指向jquery,它住在node_modules

我有这个

import {MyModule} from './components/myModule.js';
import jQuery from './jquery/jquery-3.3.1.js';

MyModule 按预期工作(在 Chrome 上)但 jQuery 产生:

未捕获的语法错误:请求的模块未提供名为“默认”的导出

jquery-3.3.1.js 是来自 jQuery.com 的 uncompressed, development jQuery 3.3.1 版本,我也尝试过生产版本。

我应该如何导入它?

编辑:

不是How to import jquery using ES6 syntax? 的重复。因为import {$,jQuery} from 'jquery';node_modules 上搜索,当我从给定路径导入它时,我得到上面引用的错误。

【问题讨论】:

  • 仅尝试import './jquery/jquery-3.3.1.js';。要导入 from 它,jQuery 需要使用 ES6 模块语法
  • 为什么不用jquery npm包然后import $ from "jquery";
  • @Bergi 你是对的!谢谢。
  • @newman 不,不是。那个和其他问题/答案总是指向node_modules

标签: javascript jquery ecmascript-6


【解决方案1】:

你应该使用:

import * as jQuery from './jquery/jquery-3.3.1.js'

关于导入声明的文档: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Instructions/import

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 2016-10-30
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2017-09-24
    相关资源
    最近更新 更多