如何在Ember.JS应用程序中加载Require.JS开发的模块?

2jcobegt  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(146)

我正在尝试将一个用Require.JS开发的第三方库集成到一个新的Ember.JS应用程序中。我看过像ember-cli-amd和ember-auto-import这样的插件,但我不知道如何让它们为这样加载的库工作

<script data-main="jslib/app/LibConfig" type="text/javascript" src="jslib/modules/require.js"></script>
3vpjnl9f

3vpjnl9f1#

欢迎使用Stack Overflow!
所以你不能直接在Ember中使用require.js,但是Ember加载器会为你构建一个require.js版本。你只需要确保你要使用的库是命名的AMD或UMD格式,然后你可以使用app.import从你的ember-cli-build文件中导入它们,如下所示:

// ember-cli-build.js

'use strict';

const EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function(defaults) {
  let app = new EmberApp(defaults, {
    // Add options here
  });

  // Use `app.import` to add additional libraries to the generated
  // output files.
  //
  // If you need to use different assets in different
  // environments, specify an object as the first parameter. That
  // object's keys should be the environment name and the values
  // should be the asset to use in that environment.
  //
  // If the library that you are including contains AMD or ES6
  // modules that you would like to import into your application
  // please specify an object with the list of modules as keys
  // along with the exports of each module as its value.

  app.import('vendor/my-awesome-named-amd-library.js');

  return app.toTree();
};

如需详细信息,请参阅:https://ember-cli.com/user-guide/#standard-named-amd-asset
或者,查看您要使用的库的版本是否是npm格式,ember-auto-import可以找出格式并为您完成工作。

相关问题