在ember-cli中导入自定义库 [英] Import custom library in ember-cli
问题描述
我在iframe中有网络应用程序
和 Ember应用程序
。我想将自定义库从 web应用程序
导入到 ember app
像全局变量。图书馆看起来像具有函数的对象:
I have web app
and Ember app
in iframe. I want to import custom library from web app
to ember app
like global variable. Library look like object with function:
var helpers = {
helper1: function() {},
helper2: function() {}
};
图书馆使用套接字,对于这两个应用程序都是一样的。
Library working with sockets and will the same for both applications.
我写道:
app.import("./../../../public/scripts/js-helpers.js"); //file located in web app
我无法访问变量 helpers
在项目和ember-cli复制文件夹 public
到他们的工作文件夹。抱歉,如果问题是愚蠢的,我不明白 ember-cli
导入原则。
I can't access variable helpers
in project and ember-cli copy folder public
into their working folder. Sorry if question is stupid, I can't understand ember-cli
import principles.
推荐答案
我会提出一个解决方案,我觉得是最简单的。您不会有全局作用域变量,但您可以使用ES6模块方法。
I'll propose one solution, which I find to be the easiest. You won't have globally scoped variables, but you use then ES6 module approach.
举个例子,假设你有一个名为 app_name / lib /
helper.js 。 lib
是您创建用于保存自定义库的目录。您不必使用该名称。使用 export default
命令:
For example purposes, let's say you have a file under app_name/lib/
called helper.js
. lib
is a directory you create for holding your custom libraries. You don't have to use that name. Use the export default
command as such:
export default {
helper1: function() {},
helper2: function() {}
};
然后,让我们说你需要一个路由。这可能是一条这样的路线:
Then, lets say you need this in a route. Here could be one such route:
import helper from 'app_name/lib/helper';
export default Ember.Route.extend({
model: function(){
helper.helper1();
}
helper
是您导入的名称,表示您导出的对象在您的 helper.js
中,此方法似乎是Ember-CLI建议您使用
helper
is the name of what you imported, and represents the object that you exported in your helper.js
. This method seems to be the style in which Ember-CLI suggests you should use
这篇关于在ember-cli中导入自定义库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!