ES6:“从'jquery'导入$"的含义是什么?真正意思? [英] ES6: What does "import $ from 'jquery'" really means?

查看:90
本文介绍了ES6:“从'jquery'导入$"的含义是什么?真正意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先假设它只是意味着,加载jQuery模块并在名为$的变量中对其进行初始化.

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

但是,然后,通过将Atom与atom-typescript一起使用,我收到一条错误消息,提示它无法找到模块'jquery'".即使所有代码都可以在浏览器中运行,但看起来atomic-typescript无法解析任何类似于从y导入x的内容.

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.

现在,查看ES6文档,我发现您是从模块导入类/函数的.含义完全不同,例如,这是有意义的:

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';

但是对于jQuery,这意味着什么呢?

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 :)

推荐答案

语句 import from jquery 相当于依赖注入.就像从"react" 编写 import React来给自己访问文件中的React库一样,从jquery 编写 import $也可以.(以防万一,请使用美元符号,因为jQuery及其方法是使用美元( a.k.a. jQuery)运算符访问的.

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. 如果您将jQuery作为依赖项单独安装在 package.json 文件中,并且从jQuery CDN中包含< script> 标记,则此错误将被抛出.如果您是通过NPM使用jQuery,那么从jquery导入 $$ 语法是正确/必要的.如果打算通过CDN使用jQuery(我建议这样做),则不需要 import 语句.(由于您已经在 index.html 中包含了该脚本标签,因此您可以在整个应用程序范围内访问jQuery及其库).但是,不要两者都做.

  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 recommend), 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.

还请注意,对于从'angular2/core'; 语句导入 import {组件}的情况,情况有所不同.即,根据(

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.

可以肯定的是,请检查您是否实际上已经通过CDN或通过将其安装为NPM依赖项来访问jQuery.

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.

这篇关于ES6:“从'jquery'导入$"的含义是什么?真正意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆