Difference between import and import from

我怕爱的太早我们不能终老 提交于 2019-12-12 02:42:24

问题


I'm looking through some ES6/browserify tutorials and I see something like:

import 'jquery';
import domready from 'domready';

What is the difference between import and import from?

If I use just import domready - domready fails to work.


回答1:


Your first line

import 'jquery';
// is functionally equivalent to
require('jquery');

Your second line

import domready from 'domready';
// is technically equivalent to
var domready = require('domready');

So you can probably see why the second one is the only one that works for you.




回答2:


Please have a look at- mozilla reference for import

Posting as answer because I do not have enough reputation to make a comment. Hope it helps.




回答3:


The reason jQuery still works is because of what jquery does. If you look at the source of the jquery library, it sets itself as window.jQuery. This creates the jQuery and $ variables in the global scope, so you can still use the variables jQuery and $. the domready library does not. It returns an instance of itself, so you have to save that to a variable using import from.



来源:https://stackoverflow.com/questions/33107689/difference-between-import-and-import-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!