简体   繁体   English

如何从所需文件中要求?

[英]How to require from required file?

I need to require bar from bar.js and foo from foo.js in each file. 我需要在每个文件中都要求bar.js中的bar和foo.js中的foo。

How I can do it? 我该怎么办?

foo.js foo.js

const bar = require('./bar');
const foo = 'Hello';
console.log(`${foo} ${bar}!`);
module.exports = foo;

bar.js bar.js

const foo = require('./foo');
const bar = 'World';
console.log(`${foo} ${bar}!`);
module.exports = bar;

Main purpose is for have many to many GraphQL connection. 主要目的是用于具有多对多的GraphQL连接。

I want to require in rootQuery separate files with lets say postType.js and tagType.js. 我想在rootQuery中要求使用让我们说postType.js和tagType.js的单独文件。 And I need to query posts from tags, and tags from posts. 我需要从标签查询帖子,并从帖子查询标签。

Is it possible? 可能吗?

Yes this is possible node.js has built in mechanisms for handling just this sort circular require() calls. 是的,这是可能的。node.js内置了用于处理这种循环通告require()调用的机制。 Please read the node documentation. 请阅读节点文档。 https://nodejs.org/api/modules.html#modules_cycles https://nodejs.org/api/modules.html#modules_cycles

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

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