且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Ember.js上导入库?

更新时间:2022-04-23 01:33:54

第一首选项:Ember Addon



优选地,您的JavaScript库将是一个余烬附加组件。然后您只需输入以下命令即可安装:

First Preference: Ember Addon

Preferably your JavaScript library will be an ember add-on. Then you can install it by simply typing:

# ember install <addon name>

这通常会处理您需要执行的所有导入。 JavaScript代码将包含在您编译的Ember应用程序中。

This will usually take care of all importing that you need to do. The JavaScript code will be included in your compiled Ember app.

如果有这是一个ember附加组件,然后你可以使用bower:

If there isn't an ember add-on, then you can use bower:

# bower install -S <bower package name>

然后你需要在 .ember-cli-build中添加依赖项 file:

Then you need to add the dependency in your .ember-cli-build file:

/*jshint node:true*/
/* global require, module */
var EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function(defaults) {
  var app = new EmberApp(defaults, {
      // snipped out some stuff
      });

  // this library is in your bower_components/somelibrary/somelibrary.js file
  app.import(app.bowerDirectory + '/somelibrary/somelibrary.js');

  return app.toTree();
};



最后的偏好:手动导入



如果如果您无法找到所需的库作为ember附加程序或bower程序包,则必须手动导入库。

Last Preference: Manual import

If you can't find your required library as an ember add-on or bower package, you're going to have to import the library manually.

步骤1 :将javascript文件夹保存在供应商文件夹中

Step 1: save the javascript folder in your vendor folder

将Clustererer2.js文件保存在文件夹中喜欢 vendor / clusterer / clusterer2.js

Save your Clustererer2.js file in a folder like vendor/clusterer/clusterer2.js.

第2步:修改 .ember-cli-build 将此文件包含在已编译的Ember应用程序中

Step 2: Modify your .ember-cli-build file to include this in your compiled Ember app

修改您的文件如下:

/*jshint node:true*/
/* global require, module */
var EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function(defaults) {
  var app = new EmberApp(defaults, {
      // snipped out some stuff
      });

  app.import('vendor/clusterer/clusterer2.js');

  return app.toTree();
};

第3步:让JSHint对新的全球

您将不得不对要在代码中引用的新全局变量感到高兴 jshint 。将其添加到 .jshintrc 文件中:

You're going to have to make jshint happy about the new global variable you're going to reference in your code. Add it in your .jshintrc file:

{
  "predef": [
    "document",
    "window",
    "-Promise",
    "Clusterer"
  ],
  "browser": true,
  "boss": true,
  // snipped a lot of stuff
  "esnext": true,
  "unused": true
}

请注意,在 - Promise条目之后我添加 Clusterer 行?

Notice that after the "-Promise" entry I added the Clusterer line?

步骤4:重建Ember应用并使用新库

现在您已经在编译输出中包含了javascript文件,您应该可以在代码中引用它。

Now that you've included the javascript file in your compiled output, you should be able to reference it in your code.