【问题标题】:How to check if a Meteor package is currently installed?如何检查当前是否安装了 Meteor 包?
【发布时间】:2016-05-29 07:30:52
【问题描述】:

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

只有这样才能使用另一个模板(这是这个包的一部分)。

类似这样的:

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

我该怎么做?

【问题讨论】:

    标签: javascript meteor meteor-blaze


    【解决方案1】:

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

    因此,您可以使用类似的东西

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

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

    【讨论】:

      【解决方案2】:

      假设您的包使用在您的项目中可访问的对象/变量。您只需要为解决方法编写一个助手

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

      【讨论】:

      • 你能给我举个例子,你将如何创建PackageObj吗?
      • 我/你不会创建对象。使该对象可访问的是包作者。这里PackageObj只是一个插图
      猜你喜欢
      • 2013-03-23
      • 2010-11-06
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 2020-10-01
      • 1970-01-01
      • 2017-08-29
      相关资源
      最近更新 更多