什么是 npm 包 ember-auto-import?
ember-auto-import 是一个方便的 npm 包,提供了在 Ember.js 应用程序中自动导入模块(例如:npm包,Ember插件,服务等)的功能,这样你就不必在你的应用程序中进行手动导入了。
同时,它还提供了将自动导入的模块和关键字和模板模板预订,使其在运行时加载和编译。
如何使用 ember-auto-import
安装 ember-auto-import
像其他 npm 包一样,你可以使用 yarn 或 npm 来安装它:
yarn add ember-auto-import // or npm install ember-auto-import --save
使用 ember-auto-import
要使用 ember-auto-import 添加自动导入功能,请按照以下步骤操作:
- 打开 app.js 文件,添加以下内容。
import App from './app'; import autoImport from 'ember-auto-import'; import resolver from './resolver'; import loadInitializers from 'ember-load-initializers'; import config from './config/environment'; autoImport(App, config);
- 使用 ember-cli-babel 插件来为你在你的应用程序中编写的所有代码启用 Babel 编译。 在 ember-cli-build.js 文件中,请添加以下内容。
'ember-cli-babel': { includePolyfill: true }
- 在你的应用程序中使用所需的 npm 包。 ember-auto-import 将自动导入和编译它们。
import moment from 'moment';
更多使用实例
Ember 插件
在你的 Ember 应用中添加 Ember 插件的步骤与添加 npm 包的步骤相同。
import { PowerCalendar } from 'ember-power-calendar';
环境变量
如果你的环境中有许多变量,而不是只有导入模块,如:
import { BroccoliPlugin } from 'broccoli-plugin'; import { cacheKeyForTree } from 'broccoli-kitchen-sink-helpers';
你需要把它们添加到 appConfig.autoload
中。
-- -------------------- ---- ------- --- --------- - - -- --- ----------- - ------ - ------------- -------- -- -------- - ---------- - --- ------- -- -- --------------- ----------------------------- -- --- --- ---- --------- - ------------------ ----------------- -------------------------------- - --- ---------- ------- ------------------ -- -- -- --
延迟加载
ember-auto-import 提供了一个方便的 API 来实现延迟加载。
-- -------------------- ---- ------- ------ --------- ---- ------------------- ------ - ----- - ---- ----------------- ------ - ----- - ---- ------- ------ - ------ - ---- ---------------- ------ - ----------- ------------- - ---- -------------------- ------ ------- ------------------ ----- ------ - -------------------------- -- ---- ----- ------------ ------------ --- --- - ----- --------------------------- -- ---- ----- ------ ------ ----------------- ---------- - ----- ---------------------- -- ----------- - ---------------- - -------- --- ------ - --------------------------------- ----------- - ------------------ ---------- - ----------------- ------------ - ----- ------------- - -- -- --------------------------- ------------------------------------------------------------- -- ---------------- - ------ -------------------------------- -- -------------- ------------- - ------ -- -- ---- - --- -- ---
总结
Ember-auto-import 是一个非常有用的工具,可以使开发者更加高效。在本文中,我们介绍了使用它的基本步骤以及更高级的使用方法。
希望本文能够帮助你学习和理解 ember-auto-import,并在你的工作中得到更多的指导与帮助。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/59790