ember-cli用bower添加依赖项

8 ember.js bower ember-cli

所以 - 我想在ember应用程序中使用typeahead.

我得到一个cli应用程序并运行然后我运行

bower install typeahead.js
Run Code Online (Sandbox Code Playgroud)

我可以看到代码已被放入bower_components中.

然后我将以下内容添加到brocfile:

/* global require, module */

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

var app = new EmberApp();

// 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('bower_components/typeahead.js/dist/typeahead.bundle.min.js');

module.exports = app.toTree();
Run Code Online (Sandbox Code Playgroud)

但它不起作用 - 我明白了

Uncaught ReferenceError: Bloodhound is not defined 
Run Code Online (Sandbox Code Playgroud)

从阅读文档 - 用bower安装并在brocfile中添加行应该足以满足它?我读错了还是这个错误?

我创建了一个公共GIT仓库,显示了这个问题:

https://github.com/wayne-o/ember-cli-bootstrap

我所做的就是:

ember new bootstrap-test
bower install bootstrap
Run Code Online (Sandbox Code Playgroud)

然后补充说:

app.import('bower_components/bootstrap/dist/css/bootstrap.css');
app.import('bower_components/bootstrap/dist/js/bootstrap.js');
Run Code Online (Sandbox Code Playgroud)

到brockfile ...

它没有奏效......

Dha*_*iri 5

您没有共享您的Brocfile.js,但是当我module.exports = app.toTree();在该文件末尾的行之后添加依赖项时,我遇到了类似的问题.文档对此并不十分清楚,但module.exports = app.toTree();应始终在Brocfile.js中排在最后.尝试将您的app.import()语句移到此行之上,事情应该正常工作.

更新

拉下你的回购我发现了一些问题.首先,你需要将--save-dev你的bower安装程序传递给bootstrap和typeahead.js,以便在其他人下载你的repo时安装它们.这将为你的bower.json增加一个这样的部分:

"devDependencies": {
   "bootstrap": "~3.2.0",
   "typeahead.js": "~0.10.5"
 }
Run Code Online (Sandbox Code Playgroud)

我还补充"Bloodhound": trueprefdef.jshintrc避免对构建jshint错误的部分:

 "predef": {
    "document": true,
    "window": true,
    "-Promise": true,
    "Bloodhound": true
  },
Run Code Online (Sandbox Code Playgroud)

您还可以替换您的$引用index.js,Ember.$以避免另一个jshint错误.

一旦我这样做了,我就可以运行ember serve并让应用程序加载而没有任何Bloodhound问题.

  • 关键是您需要在添加依赖项后重新启动服务器 (3认同)