节点模块可以相互需要 [英] Can node modules require each other

查看:64
本文介绍了节点模块可以相互需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下3个文件。

user.js需要room.js而room.js需要user.js。

user.js requires room.js and room.js requires user.js.

user.js

var Room = require('./room.js');

var User = function () {};
User.prototype.test = function () {
  return new Room();
};

module.exports = User;

room.js

var User = require('./user.js');

var Room = function () {};
Room.prototype.test = function () {
  return new User();
};

module.exports = Room;

index.js

var User = require('./user.js');
var Room = require('./room.js');

var user = new User();
var room = new Room();

user.test();
room.test();

index.js需要房间和用户。

index.js requires both room and user.

这是问题所在。当我运行index.js时,我将从room.js中的'new User()'获取一个TypeError。看来用户在room.js中的用户在index.js中隐藏了。

Here's the problem. When I run index.js, I will get a TypeError from 'new User()' in room.js. It seems that User in room.js is hidden by the User in index.js.

我做错了什么?是否允许这种要求?有任何想法吗?谢谢。

Am I doing anything wrong? Is this kind of requiring allowed? Any ideas? Thanks.

推荐答案

查看 http://nodejs.org/api/modules.html#modules_cycles ,了解如何在节点中处理此问题。

Check out http://nodejs.org/api/modules.html#modules_cycles for how this is handled in node.

您可以通过多种方式解决问题,例如将依赖项传递给实例,即依赖注入

You can solve your problem in several ways, for example passing in the dependencies to the instances aka Dependency Injection

// user.js
var User = function (Room) { this.Room = Room; };
User.prototype.test = function () {
  return new this.Room();
};
module.exports = User;

// room.js
var Room = function (User) { this.User = User; };
Room.prototype.test = function () {
  return new this.User();
};
module.exports = Room;

// index.js
var User = require('./user.js');
var Room = require('./room.js');

var user = new User(Room);
var room = new Room(User);

另一种方法是仅在需要时才需要文件

Another way would be to require the files only when you need them

// user.js
var User = function () {};
User.prototype.test = function () {
  var Room = require('./room');
  return new Room();
};
module.exports = User;


// room.js
var Room = function () {};
Room.prototype.test = function () {
  var User = require('./user');
  return new User();
};
module.exports = Room;

// index.js
var User = require('./user.js');
var Room = require('./room.js');

var user = new User();
var room = new Room();

像这样,您的出口是根据您需要的时间定义的。

Like this, your exports are defined by the time you need them.

但一般来说,如果你有循环依赖,你做错了,应该考虑你的架构。
如果用户需要创建新的房间房间需要创建新的用户,看起来他们都有太多的责任。
可能你需要第三个组件负责创建并将正确的实例传递给房间用户,而不是直接将它们实例化。

But generally, if you have circular dependencies, you are doing something wrong and should think about your architecture. If a User needs to create new Rooms and a Room needs to create new Users, it seems like they both have too much responsibility. Possibly you would want a third component which is responsible for creating and passing the right instances to the Room and User, instead of having them instantiate those directly.

这篇关于节点模块可以相互需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆