最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ES6: What does "import $ from 'jquery'" really means? - Stack Overflow

programmeradmin1浏览0评论

I assumed at first that it simply means, load the jQuery module and initialize it in a variable called $.

But then, by using Atom with the atom-typescript, I got an error saying that it "Cannot find module 'jquery'". Even though all the code works in the browser, it looks like atom-typescript can't resolve anything looking like import x from y.

Now, looking at ES6 doc, I found out that you import a class/function from a module. The meaning is totally different, and it makes sense with for example this:

import { Component } from 'angular2/core';

But then what does it mean in the case of jQuery?

I am probably mixing different issues in the same one but any explanation would clear this confusion, so thanks a lot in advance :)

I assumed at first that it simply means, load the jQuery module and initialize it in a variable called $.

But then, by using Atom with the atom-typescript, I got an error saying that it "Cannot find module 'jquery'". Even though all the code works in the browser, it looks like atom-typescript can't resolve anything looking like import x from y.

Now, looking at ES6 doc, I found out that you import a class/function from a module. The meaning is totally different, and it makes sense with for example this:

import { Component } from 'angular2/core';

But then what does it mean in the case of jQuery?

I am probably mixing different issues in the same one but any explanation would clear this confusion, so thanks a lot in advance :)

Share Improve this question asked Nov 11, 2016 at 2:51 TigrouMeowTigrouMeow 3,7994 gold badges28 silver badges32 bronze badges 2
  • 2 The bit of information you're missing is regarding default exports from modules. You also need to have a TypeScript definition for jQuery in your project. – Marty Commented Nov 11, 2016 at 2:54
  • developer.mozilla/en/docs/web/javascript/reference/… – Phil Commented Nov 11, 2016 at 3:02
Add a ment  | 

1 Answer 1

Reset to default 3

The statement import $ from jquery pretty much amounts to dependency injection. Just like one would write import React from 'react' to give oneself access to the React library within a file, so to can one write import $ from jquery. (In case it's throwing you off, the dollar sign is used because jQuery and its methods are accessed using the dollar (a.k.a. jQuery) operator.

As for the errors being thrown, that could be several things:

  1. If you separately installed jQuery as a dependency in your package.json file as well as included a <script> tag from a jQuery CDN, this error will be thrown. If you're usage of jQuery is through NPM, then the import $ from jquery syntax is correct/necessary. If you intend to use jQuery through a CDN (as I would remend), the import statement is unnecessary. (Since you've included that script tag in your index.html, you have access to jQuery and its library throughout the scope of your application). Do not, however, do both.

  2. Also note that in the case of the import { Component } from 'angular2/core'; statement, something slightly different is going on. Namely, one is importing the named export Component, as per the (AMD specification. You can think of it, in this case, as importing only a part of the larger Angular2 core library when the entire library would be unnecessary.

Just to be sure, check that you have actually given yourself access to jQuery through either a CDN or by installing it as an NPM dependency.

发布评论

评论列表(0)

  1. 暂无评论