久久久久久久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 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 91蝌蚪少妇偷拍 | 国产三级视频在线播放 | 国产免费久久 | 久久综合久久鬼 | 欧美在线 | 五月婷婷在线视频 | 99国产精品99久久久久久 | 欧美综合在线视频 | 污视频网站在线观看 | 日韩成人中文字幕 | 久久99热这里只频精品6学生 | 国产欧美久久久 | 成av人片在线观看www | 国产精品麻豆 | 91成人亚洲 | 欧美日韩国产一区二区 | 黄色高清网站 | www.草逼| www.4hu.tv4| 人人爱av| 午夜性色 | 久久精品国产77777蜜臀 | 视频一区二区在线播放 | 国产吃瓜黑料一区二区 | 天天插天天 | 一区二区免费看 | 欧美激情久久久 | 久久在线 | 九九天堂| 中文字幕手机在线观看 | 国产精品日韩欧美 | 欧美激情网站 | 国产精品久久午夜夜伦鲁鲁 | 成人小视频在线观看 | 久久久久毛片 | 日本午夜视频 | 色哟哟一区二区 | 亚洲精品视频免费 | 国产综合亚洲精品一区二 | 欧美成人精品欧美一级乱黄 | 激情五月综合色婷婷一区二区 |