如何从Meteor包导出到我的应用程序命名空间? [英] How can I export from a Meteor package into my app's namespace?

查看:101
本文介绍了如何从Meteor包导出到我的应用程序命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何撰写 Meteor包但我似乎无法弄清楚如何让所有出口都落在我的应用程序命名空间中,如上所述在此演示文稿中

I know how to write Meteor packages but I can't seem to figure out how to have all exports land in my app's namespace, as described in this presentation.

此特定package特定于我正在构建的应用程序,它只导出一个可以被视为应用程序单例的装饰器的方法。我尝试了 api.export('MyApp.myMethod'),但这会产生错误 native:错误的导出符号:MyApp.myMethod

This particular package is specific to an app I'm building, and it exports only one method that can be regarded as a decorator on the app's singleton. I tried api.export('MyApp.myMethod') but that gives an error native: Bad exported symbol: MyApp.myMethod.

如果我只是 api.export('myMethod'),那么在应用程序代码中我有调用 myMethod(),这不是命名空间。

If I just api.export('myMethod'), then in the app code I have to call myMethod(), and that's not namespaced.

Meteor是否有类似于Node <$ c的机制$ c> var http = require('http'); ?或者如何将包导出到给定的命名空间?

Does Meteor have a mechanism similar to Node's var http = require('http');? Or how can packages export symbols into a given namespace?

推荐答案

api.export 方法仅支持 顶部现在的级别变量 。它不适用于嵌套变量,因为结果表明使用深度导出是
非常混乱
,你会期望什么 MyApp.myMethod MyApp.myOtherMethod ,那么code>将显示在全局命名空间中?

The api.export method is only supported for top-level variables right now. It doesn't work for nested variables, because "it turned out that using deep exports was very confusing", and what would you expect MyApp.myMethod to appear as in the global namespace if you also exported MyApp.myOtherMethod?

你应该只需导出 MyPackage ,然后调用 MyPackage.myMethod()。一种常见的方法是执行类似

You should just export MyPackage, and then call MyPackage.myMethod(). A common approach is to do something like

MyPackage = { 
    myMethod: someSecretMethodName,
    myOtherMethod: otherMethodName
}

然后拨打 api.export(MyPackage )。这意味着变量的导出名称不一定必须是您所称的变量名称。这在核心流星包中经常使用;您还可以在 mongo_driver.js中的MongoInternals 中查看示例。

And then call api.export("MyPackage"). This means that the exported names of variables don't necessarily have to be what you called them. This is used a lot in the core meteor packages; you can also see an example at for MongoInternals in mongo_driver.js.

这篇关于如何从Meteor包导出到我的应用程序命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆