Skip to content

资源

¥Assets

目前,无法将 Assets 作为 ES6 模块导入。下面的任何 Assets 方法都可以在任何 Meteor 服务器代码中直接调用。

¥Currently, it is not possible to import Assets as an ES6 module. Any of the Assets methods below can simply be called directly in any Meteor server code.

Assets 允许 Meteor 应用中的服务器代码访问静态服务器资源,这些资源位于应用树的 private 子目录中。资源不会作为源文件进行处理,而是直接复制到应用的包中。

¥Assets allows server code in a Meteor application to access static server assets, which are located in the private subdirectory of an application's tree. Assets are not processed as source files and are copied directly into your application's bundle.

Assets.getTextAsync

Server only

Summary:

Retrieve the contents of the static server asset as a UTF8-encoded string.

Arguments:

Source code
NameTypeDescriptionRequired
assetPathString

The path of the asset, relative to the application's private subdirectory.

Yes
asyncCallbackfunction

Optional callback, which is called asynchronously with the error or result after the function is complete. If not provided, the function runs synchronously.

No
js



const result = Assets.getTextAsync();
  "assetPath",
() => {}, // this param is optional
);

Assets.getBinaryAsync

Server only

Summary:

Retrieve the contents of the static server asset as an EJSON Binary.

Arguments:

Source code
NameTypeDescriptionRequired
assetPathString

The path of the asset, relative to the application's private subdirectory.

Yes
asyncCallbackfunction

Optional callback, which is called asynchronously with the error or result after the function is complete. If not provided, the function runs synchronously.

No
js



const result = Assets.getBinaryAsync();
  "assetPath",
() => {}, // this param is optional
);

Assets.absoluteFilePath

Server [Not in build plugins] only

Summary:

Get the absolute path to the static server asset. Note that assets are read-only.

Arguments:

Source code
NameTypeDescriptionRequired
assetPathString

The path of the asset, relative to the application's private subdirectory.

Yes
js



const result = Assets.absoluteFilePath();
  "assetPath"
);

将静态服务器资源放置在应用的 private 子目录中即可将其包含在内。例如,如果应用的 private 子目录包含一个名为 nested 的目录,其中包含一个名为 data.txt 的文件,则服务器代码可以通过运行以下命令读取 data.txt

¥Static server assets are included by placing them in the application's private subdirectory. For example, if an application's private subdirectory includes a directory called nested with a file called data.txt inside it, then server code can read data.txt by running:

js
const data = await Assets.getTextAsync('nested/data.txt');

注意:包只能访问自己的资源。如果你需要读取不同包或封闭应用的资源,则需要获取对该包的 Assets 对象的引用。

¥Note: Packages can only access their own assets. If you need to read the assets of a different package, or of the enclosing app, you need to get a reference to that package's Assets object.