如何检查当前是否安装了 Meteor 包?

How to check if a Meteor package is currently installed?

我需要检查我的应用程序中是否安装了包 my-package:notification

然后才使用另一个模板(此包的一部分)。

像这样:

<template name="example">
    {{#if hasNotificationPackage}}
        {{>notification}}
    {{/if}}
</template>

我该怎么做?

假设您的包使用了在您的项目中可访问的 object/variable。您只需要为解决方法编写一个帮助程序

Template.example.helpers({
hasNotificationPackage: function(){
         return (PackageObj) ? true: false; //PackageObj is your package permissible object
    }

});

Meteor 提供了一个名为 Package 的全局对象,其中包含所有 Meteor 包导出。

因此,您可以使用类似

的东西
Template.example.helpers({
  hasNotificationPackage() {
    return (typeof Package['my-package:notification'] === 'object');
  }
});

我不确定是否应该经常使用这种方式取决于包的可用性,尤其是在生产中。