久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 ES6 導入時“未定義 jQuery"

#39;jQuery is not defined#39; when use ES6 import(使用 ES6 導入時“未定義 jQuery)
本文介紹了使用 ES6 導入時“未定義 jQuery"的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的代碼:

import $ from 'jquery'
import jQuery from 'jquery'
import owlCarousel from '../../node_modules/owlcarousel/owl-carousel/owl.carousel'

class App {
    …
    _initSlider() {
        $("#partners-carousel").owlCarousel();
    }
}

我在瀏覽器控制臺中有未定義 jQuery".怎么了?我可以在此類的方法中使用 jQuery 作為 $,但不能使用名稱 'jQuery'.

I have 'jQuery is not defined' in browser console. What's wrong? I can use jQuery as $ in methods of this class, but not with name 'jQuery'.

推薦答案

根據此評論并將其應用于您的案例,當您這樣做時:

According to this comment and apply it to your case, when you're doing:

import $ from 'jquery'
import jQuery from 'jquery'

您實際上并沒有使用命名導出.

you aren't actually using a named export.

問題在于,當您執行 import $ ...import jQuery ... 然后 import 'owlCarousel'(其中依賴于 jQuery),這些都是在之前評估的,即使你在導入 jquery 之后立即聲明 window.jQuery = jquery.這是 ES6 模塊語義不同于 CommonJS 的 require 的方式之一.

The problem is that when you do import $ ..., import jQuery ... and then import 'owlCarousel' (which depends on jQuery), these are evaluated before, even if you declare window.jQuery = jquery right after importing jquery. That's one of the ways ES6 module semantics differs from CommonJS' require.

解決此問題的一種方法是改為這樣做:

One way to get around this is to instead do this:

創建文件jquery-global.js

// jquery-global.js
import jquery from 'jquery';
window.jQuery = jquery;
window.$ = jquery;

然后將其導入主文件:

// main.js
import './jquery-global.js';
import 'owlCarousel' from '../../node_modules/owlcarousel/owl-carousel/owl.carousel'

class App {
  ...
  _initSlider() {
    $("#partners-carousel").owlCarousel();
  }
}

這樣可以確保在加載 owlCarousel 之前定義了全局 jQuery.

That way you make sure that the jQuery global is defined before owlCarousel is loaded.

這篇關于使用 ES6 導入時“未定義 jQuery"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創建頭像的 jQuery/JavaScript 庫?)
How to do following mask input problem?(如何做以下掩碼輸入問題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設置值/標簽的問題)
how to unit-test private methods in jquery plugins?(如何對 jquery 插件中的私有方法進行單元測試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動網站配置偏移量/對齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 美女视频一区二区 | 日批av| 黄色一级大片在线免费看产 | 成人福利网 | 久久99久久98精品免观看软件 | 亚洲综合字幕 | 久久久一区二区三区 | 91一区| 国产免费一区二区 | 久久久久久久一区 | 欧美精品在线免费 | 免费在线一区二区三区 | 欧美一区成人 | 伊人网站| 欧美一区在线视频 | 国产一区二区三区色淫影院 | 盗摄精品av一区二区三区 | 欧美高清视频 | 中文字幕一区二区在线观看 | 日韩欧美在线免费 | 精品视频久久久久久 | 五月天激情电影 | 草久网 | 久久精品久久久 | 欧美日韩视频 | 一区二区三区亚洲 | 免费午夜视频在线观看 | 日韩欧美国产精品一区二区 | 欧美性成人 | 在线综合视频 | 中文字幕乱码一区二区三区 | 夜夜撸av| 精品国模一区二区三区欧美 | 99福利在线观看 | 一级黄a| 日韩久久久一区二区 | 国产欧美在线播放 | 欧美一区二区激情三区 | 欧美日韩综合一区 | 中文字幕在线免费观看 | 一级黄色影片在线观看 |