如何在流星包中加入一个变量(函数)?

时间:2022-10-07 22:35:02

I have a meteor package installed in my Meteor app

我在我的流星应用中安装了一个流星包

/packages/mypackage/some_code.js

inside is a function:

里面是一个函数:

function myFunction(){
    ...
}

i would like to call this function from another file inside my metoer app. Both codes are running on serverside.

我想从我的metoer应用程序中的另一个文件调用这个函数。这两个代码都在服务器端运行。

/imports/api/myapi.js

How can i access myFunction from myapi.js ?

如何从myapi访问myFunction。js ?

UPDATE:

更新:

After lots of try and error my export looks like this:

经过多次尝试和错误之后,我的出口看起来是这样的:

var exports = module.exports = {};

exports.getResponses = function (){
    return responses;
}

and import:

和导入:

import { getResponses } from 'meteor/user:mypackage/myfile.js' ;

1 个解决方案

#1


2  

This topic is covered by the Meteor Guide on Exporting. In that guide:

本主题由《流星指南》所涵盖。在指南:

While some packages exist just to provide side effects to the app, most packages provide a reusable bit of code that can be used by the consumer with import. To export a symbol from your package, simply use the ES2015 export syntax in your mainModule:

虽然有些包的存在只是为了给应用程序提供副作用,但大多数包都提供了可重用的代码,用户可以使用这些代码进行导入。要从包中导出符号,只需在主模块中使用ES2015导出语法:

// in my-package.js:
export const myName = 'my-package';

Now users of your package can import the symbol with:

现在您的包的用户可以用以下方式导入符号:

import { myName } from 'meteor/username:my-package';

This works for internal packages found at the /package directory as well. Just make sure you have the ecmascript core package, and that you are exporting/importing your objects correctly. You will also need to add the name of the package in .meteor/packages through meteor add <package> or by entering it in manually.

这也适用于在/package目录中找到的内部包。只要确保您有ecmascript核心包,并且您正在正确地导出/导入对象。您还需要通过流星添加 或手动输入。流星/包添加包的名称。

Edit - some elaboration on import/export syntax - hopefully this will help you debug the undefined issues!

编辑-一些关于导入/导出语法的详细说明-希望这将帮助您调试未定义的问题!

From MDN's guide on import/export:

MDN的进出口指南:

There are two types of exports, named and default.

有两种类型的导出,命名和默认。

Named exports:

叫出口:

export { myFunction }; // exports a function declared earlier
export const foo = Math.sqrt(2); // exports a constant

Default exports: (only one per script)

默认导出:(每个脚本只有一个)

export default function() {} // or 'export default class {}'
// there is no semi-colon here

Named exports allow exporting multiple values per script, while exporting a default allows one per script, or a fallback.

命名导出允许在每个脚本中导出多个值,而导出默认值允许每个脚本执行一个值,或者一个后退。

For example, to import from multiple named exports, you would write:

例如,要从多个已命名的导出中导入,您可以这样写:

import { myFunction, foo } from 'some-module.js';

Whereas importing a default value looks like:

导入默认值如下:

import defs from 'definitions.js';

#1


2  

This topic is covered by the Meteor Guide on Exporting. In that guide:

本主题由《流星指南》所涵盖。在指南:

While some packages exist just to provide side effects to the app, most packages provide a reusable bit of code that can be used by the consumer with import. To export a symbol from your package, simply use the ES2015 export syntax in your mainModule:

虽然有些包的存在只是为了给应用程序提供副作用,但大多数包都提供了可重用的代码,用户可以使用这些代码进行导入。要从包中导出符号,只需在主模块中使用ES2015导出语法:

// in my-package.js:
export const myName = 'my-package';

Now users of your package can import the symbol with:

现在您的包的用户可以用以下方式导入符号:

import { myName } from 'meteor/username:my-package';

This works for internal packages found at the /package directory as well. Just make sure you have the ecmascript core package, and that you are exporting/importing your objects correctly. You will also need to add the name of the package in .meteor/packages through meteor add <package> or by entering it in manually.

这也适用于在/package目录中找到的内部包。只要确保您有ecmascript核心包,并且您正在正确地导出/导入对象。您还需要通过流星添加 或手动输入。流星/包添加包的名称。

Edit - some elaboration on import/export syntax - hopefully this will help you debug the undefined issues!

编辑-一些关于导入/导出语法的详细说明-希望这将帮助您调试未定义的问题!

From MDN's guide on import/export:

MDN的进出口指南:

There are two types of exports, named and default.

有两种类型的导出,命名和默认。

Named exports:

叫出口:

export { myFunction }; // exports a function declared earlier
export const foo = Math.sqrt(2); // exports a constant

Default exports: (only one per script)

默认导出:(每个脚本只有一个)

export default function() {} // or 'export default class {}'
// there is no semi-colon here

Named exports allow exporting multiple values per script, while exporting a default allows one per script, or a fallback.

命名导出允许在每个脚本中导出多个值,而导出默认值允许每个脚本执行一个值,或者一个后退。

For example, to import from multiple named exports, you would write:

例如,要从多个已命名的导出中导入,您可以这样写:

import { myFunction, foo } from 'some-module.js';

Whereas importing a default value looks like:

导入默认值如下:

import defs from 'definitions.js';