mean.io dependency injection

681 Views Asked by At

How does dpendency injection in mean.io work. As per the docs http://learn.mean.io/#mean-packages-dependency-injection.

I created two packages pkg1 and pkg2. In my pkg2 app.js file I have

Pkg2.register(function(app, auth, database,Pkg1) {  
  Pkg2.routes(app, auth, database);


  Pkg2.menus.add({
    title: 'pkg2 example page',
    link: 'pkg2 example page',
    roles: ['authenticated'],
    menu: 'main'
  });

  Pkg2.aggregateAsset('css', 'pkg2.css');

  return Pkg2;
});

But I get this error when i start the application

 Error: dependency 'Pkg1' was not registered

Pkg1 is registered using the following code

Pkg1.register(function(app, auth, database) {
 Pkg1.routes(app, auth, database);
 Pkg1.menus.add({
   title: 'pkg1 example page',
   link: 'pkg1 example page',
   roles: ['authenticated'],
   menu: 'main'
 });

 Pkg1.aggregateAsset('css', 'pkg1.css');

 return Pkg1;
});

Both packages are created using commands

mean package pkg1
mean package pkg2
1

There are 1 best solutions below

0
On

When you have two packages, you can use one package in another as below.

In the Pk1, you can register the package:

var Module = require('meanio').Module;
var Package1 = new Module('pk1');

Package1.register(function(app) {
    ...
});

Then you can use the pk1 in your second package.

var Module = require('meanio').Module;
var Package2 = new Module('pk2');

Package2.register(function(app, pk1) {
    ...
});

Make sure you use pk1, not Package1. Otherwise, the meanio package system give you the package not registered error.