How can I export from a Meteor package into my app's namespace?

后端 未结 3 482
渐次进展
渐次进展 2021-02-05 21:25

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.

This particul

3条回答
  •  孤街浪徒
    2021-02-05 21:46

    All properties that you register in your app namespace are made available for the packages that depend on (use) your app-package. So, when you want to register a package namespace in an app-namespace, you declare the dependency on the app-package within your package and you register all of the methods/objects you want to export in the app-namespace. An example:

    file: packages/myapp/namespace.js

    MyApp = {};
    

    file: packages/myapp/package.js

    Package.on_use(function (api, where) {
      api.add_files([
        "namespace.js"
      ], ['client', 'server']);
      api.export("MyApp", ['client', 'server']);
    });
    

    file: packages/myapp-module1/logic.js

    packageSpecificMethod = function(){}
    moduleOne = {};
    //you can export an module-specific namespace by registering it in the app-namespace
    MyApp.module1 = moduleOne;
    //or you can (if you dont want package-namespaces) register you private methods in the app-namespace directly
    MyApp.exportedMethod = packageSpecificMethod;
    

    file: packages/myapp-module1/package.js

    Package.on_use(function (api, where) {
      api.use([
        'myapp'
      ], ['client', 'server']);
      api.add_files([
        "logic.js"
      ], ['client', 'server']);
    });
    

提交回复
热议问题