简体   繁体   English

均值依赖注入

[英]mean.io dependency injection

How does dpendency injection in mean.io work. 均值注入在mean.io中如何工作。 As per the docs http://learn.mean.io/#mean-packages-dependency-injection . 根据文档http://learn.mean.io/#mean-packages-dependency-injection

I created two packages pkg1 and pkg2. 我创建了两个软件包pkg1和pkg2。 In my pkg2 app.js file I have 在我的pkg2 app.js文件中

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

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

When you have two packages, you can use one package in another as below. 当您有两个软件包时,可以如下使用一个软件包。

In the Pk1, you can register the package: 在Pk1中,您可以注册软件包:

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

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

Then you can use the pk1 in your second package. 然后,您可以在第二个软件包中使用pk1。

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

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

Make sure you use pk1 , not Package1 . 确保使用pk1而不是Package1 Otherwise, the meanio package system give you the package not registered error. 否则,meanio软件包系统会给您未注册的软件包错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM