问题
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