Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Nodejs:只使用index.js导出的良好实践?_Javascript_Node.js_Iojs - Fatal编程技术网

Javascript Nodejs:只使用index.js导出的良好实践?

Javascript Nodejs:只使用index.js导出的良好实践?,javascript,node.js,iojs,Javascript,Node.js,Iojs,我在继承的一些代码上看到了一种模式。每个目录都有自己的JS文件,但也有一个index.JS,它实际从其他JS文件导出项目 我认为这样做是为了让您能够准确地看到您正在导出的内容,因为主要导出在index.js中,主代码在其他js文件中 这是正确的吗?这种模式叫什么 我是否应该继续使用此模式。假设我有以下目录结构: MyApp ├── app.js ├── test.js ├── package.json ├─┬ controllers │ ├── index.js │ ├── signIn.js

我在继承的一些代码上看到了一种模式。每个目录都有自己的JS文件,但也有一个index.JS,它实际从其他JS文件导出项目

我认为这样做是为了让您能够准确地看到您正在导出的内容,因为主要导出在index.js中,主代码在其他js文件中

这是正确的吗?这种模式叫什么


我是否应该继续使用此模式。

假设我有以下目录结构:

MyApp
├── app.js
├── test.js
├── package.json
├─┬ controllers
│ ├── index.js
│ ├── signIn.js
│ └── signOut.js
└─┬ views
  ├── index.js
  ├── signIn.js
  └── signOut.js
将以下代码放入
index.js
文件中

// index.js
module.exports = {
  signIn: require('./signIn')
, signOut: require('./signOut')
};
…允许您
要求
整个目录,如

// test.js
describe('controllers', () => {
  // ~/controllers/index.js
  const controllers = require('./controllers');

  it('performs a sign-in', () => {
    ...
  });
  it('performs a sign-out', () => {
    ...
  });
});
另一种方法是
单独要求每个文件

不需要在目录中具有
index.js
。您可以
要求
目录中的文件没有
index.js

// app.js
const signOut = require('./controllers/signOut.js')
然而,随着应用程序的增长,它会变得单调乏味。我使用一个像这样的软件包,在一个目录中键入每个文件也很繁琐,而且有点容易出错

// index.js
module.exports = require('require-directory')(module);

/*

This yields the same result as:

module.exports = {
  signIn: require('./signIn')
, signOut: require('./signOut')
, ...
};

*/
ES6 CommonJS模块语法 鉴于这两种常见的结构类型

MyApp
│ // files divided per type (controllers, components, actions, ...)
├─┬ actions
│ ├── index.js
│ ├── signIn.js
│ └── signOut.js
├─┬ components ...
├─┬ reducers ...
├─┬ pages ...
│ 
│ // files divided per component
├─┬ components ...
│ ├── index.js 
│ ├── SimpleComponent.jsx
│ ├── AnotherComponent.duck.jsx // redux "duck" pattern
│ ├─┬ ComplexComponent // large complex logic, own actions, stylesheet, etc.
│ ...
├─┬ pages ...
│ ├── index.js 
│ ├─┬ App
│ │ ├── index.js // not necessary here, matter of habit
│ │ ├── App.jsx
│ │ ├── actions.js
│ │ └── reducer.js
│ └─┬ Dashboard
├── another.js
...
您可以像这样在
other.js
中导入文件

import {signIn, signOut} from './actions'
import {App} from './pages'
import {ComplexComponent} from './components'
而不是这个(没有
index.js
文件)

更多阅读

-现在将新导入内容带到浏览器中



-组件的单文件方法

其他答案提供了大量有用的信息,但要尝试并具体回答您的问题“我是否应该继续使用此模式”,我会说不,至少在大多数情况下是这样

问题是,这种模式需要额外的努力,因为您必须维护那些额外的
index.js
文件。根据我的经验,这种努力比简单地编写一个更长的
import
语句要大。此外,您可以通过使用像
require dir
这样的模块

综上所述,如果您正在制作一个将被大量用户使用的库,比如大型编程部门的关键模块或公共NPM模块,那么
index.js
的工作就更有道理了。只要您有足够的人使用您的模块,您的用户就会(累积地)使用添加它们可以节省比维护它们更多的时间。

这怎么办

module.exports = {
  ...require('./moduleA'),
  ...require('./moduleB')
}

(moduleA.a将被moduleB.a覆盖)

不需要,index.js需要将包索引字段链接到索引文件。但是在模块内,您可以加载任何文件及其相对路径。它只是
require
整个目录的缩写。
module.exports = {
  ...require('./moduleA'),
  ...require('./moduleB')
}