简体   繁体   English

如何将目录设置为批量 JavaScript 导入的“模块”?

[英]How to set up a directory as a "module" for bulk JavaScript import?

Just like we'd do import * as React from 'react';就像我们将import * as React from 'react'; and then for example const [state, setState] = React.useState(false);然后例如const [state, setState] = React.useState(false);

I would like to import a bunch of my exported stuff from a folder to then access using dot-notation of the imported module.我想从一个文件夹中导入一堆我导出的东西,然后使用导入模块的点符号进行访问。

For example, an "AppBar" component using "AppsButton", "HomeButton", "BugReportButton" and "HelpButton", which I put as jsx files in the same folder.例如,使用“AppsButton”、“HomeButton”、“BugReportButton”和“HelpButton”的“AppBar”组件,我将它们作为 jsx 文件放在同一个文件夹中。 These files each "export default" functions for their views.这些文件各自为它们的视图“导出默认”功能。 I would like to do:我想要做:

 import AppBar from '@mui/material/AppBar'; import * as AppBarContent from ".//AppBar"; // hopefully the correct syntax for the folder named "AppBar", which this file sits in

and then use each component in the view like:然后使用视图中的每个组件,例如:

 <AppBar> <AppBarContent.AppsButton /> <AppBarContent.HomeButton /> <AppBarContent.BugReportButton /> <AppBarContent.HelpButton /> </AppBar>

I looked about and found this resource , but wasn't certain it's the same thing I'm after and when I made the suggested index.js with the following content it didn't change anything (I could've done it wrong).我四处寻找并找到了这个资源,但不确定它是否与我所追求的相同,当我使用以下内容制作建议的 index.js 时,它并没有改变任何东西(我可能做错了)。

index.js: index.js:

 exports.AppsButton = require("./AppsButton"); exports.HomeButton = require("./HomeButton"); exports.BugReportButton = require("./BugReportButton"); exports.HelpButton = require("./HelpButton"); exports.HelpDrawer = require("./HelpDrawer");

Any single resource or some steps how to achieve this please?任何单一资源或一些步骤如何实现这一点?

You cannot have more than one default export in one file.一个文件中不能有多个默认导出。 Use named exports as follows:使用命名导出如下:

appbar.js: appbar.js:

export const BugReport = () => <div> bug </div>
export const Header = () => <div> header </div>
export const Footer = () => <div> Footer </div>

Then you can import and use them as follows:然后您可以按如下方式导入和使用它们:

header.js header.js

import React from "react";
import * as Appbar from "./appbar";

function Header(props) {
  return (
      <div>
        <Appbar.BugReport/>
        <Appbar.Footer/>
        <Appbar.Header/>
      </div>
  );
}

export default Header;

I actually found the solution to this when checking out some coding standards for React:在查看 React 的一些编码标准时,我实际上找到了解决方案: 在此处输入图像描述

One point says "Create a index.js within each folder for exporting".有一点说“在每个文件夹中创建一个 index.js 用于导出”。 Source: https://www.jondjones.com/frontend/react/react-tutorials/react-coding-standards-and-practices-to-level-up-your-code/资料来源: https://www.jondjones.com/frontend/react/react-tutorials/react-coding-standards-and-practices-to-level-up-your-code/

After I've added an index.js to each folder that imports all components in that folder and then exports them all, I can import individual components from that folder level as a "module" (apologies if terminology incorrect) and also import all as a named source that I can access using dot notation.在我将index.js添加到导入该文件夹中所有组件然后将它们全部导出的每个文件夹之后,我可以从该文件夹级别导入单个组件作为“模块”(如果术语不正确,请致歉)并将所有组件导入为我可以使用点符号访问的命名源 Note that for this to work the imports (of indexed exports) must now include curly braces.请注意,为此,导入(索引导出)现在必须包含花括号。

For example all the buttons going into a new folder "buttons" and having an index.js :例如,所有按钮都进入一个新文件夹“buttons”并具有index.js

 import AppsButton from "./AppsButton"; import HomeButton from "./HomeButton"; import AppHomeButton from "./AppHomeButton"; import HelpButton from "./HelpButton"; export {AppsButton, HomeButton, AppHomeButton, HelpButton}

Then the AppBar.js a folder up can do either:然后AppBar.js一个文件夹向上可以做到:

 import {AppsButton, HomeButton, AppHomeButton, HelpButton} from "./buttons"
with

 <AppBar> <AppsButton /> <HomeButton /> <BugReportButton /> <HelpButton /> </AppBar>

or:或者:

 import * as buttons from "./buttons"
with: 和:

 <AppBar> <buttons.AppsButton /> <buttons.HomeButton /> <buttons.BugReportButton /> <buttons.HelpButton /> </AppBar>

(I went for the former in this case, but I can see now how in other areas of my code-base I could have things like appBarContent.buttons.AppsButton reused elsewhere using this approach...) (在这种情况下,我选择了前者,但现在我可以看到在我的代码库的其他区域中,我可以使用这种方法在其他地方重用 appBarContent.buttons.AppsButton 之类的东西......)

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

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