简体   繁体   English

将Styled-Components拆分为多个文件,但将它们导出为一个文件

[英]Splitting Styled-Components into multiple files but export them as one file

When I was building my application, I didn't realize that I am going to end up with more than a hundred styled components. 当我构建我的应用程序时,我没有意识到我最终会得到超过一百个样式的组件。 Consequently, I was putting them in the same file like this: 因此,我将它们放在同一个文件中:

export const StyledTabs = styled(Tabs)`
  display: inline-flex !important;
  margin-left: 15% !important;
`;

export const StyledTab = styled(Tab)`
  display: inline-flex !important;
  margin-left: 0% !important;
  padding: 0 !important;
`;

... And the application imports them like this: ...并且应用程序像这样导入它们:

import { StyledTabs, StyledTitle } from "StyledComponents";

I want to split the StyledComponents.js file into multiple files by, for example, UI logic (header, footer, containers, etc..) but somehow, import them back into StyledComponents.js so I don't have to refactor the entire application. 我想通过UI逻辑(页眉,页脚,容器等)将StyledComponents.js文件拆分成多个文件,但不知何故,将它们导回到StyledComponents.js这样我就不必重构整个文件了。应用。

Is something like this possible: 这样的事情是可能的:

HeaderSC.js HeaderSC.js

export const StyledTabs = styled(Tabs)`
  display: inline-flex !important;
  margin-left: 15% !important;
`;

export const StyledTab = styled(Tab)`
  display: inline-flex !important;
  margin-left: 0% !important;
  padding: 0 !important;
`;

StyledComponents.js StyledComponents.js

import { StyledTabs, StyledTitle } from "../styling/HeaderSC";

..and then the app still referring to StyledTabs or any other styled component from StyledComponents.js file? ..然后应用程序仍然引用StyledTabsStyledComponents.js文件中的任何其他样式组件?

You can create a folder StyledComponents and inside that create a default import file index.js, from which all your exports will be facilitated. 您可以创建一个文件夹StyledComponents,在其中创建一个默认导入文件index.js,从中可以方便您的所有导出。

In the very same folder create different files for different components like StyledTabs.js and StyledTitle.js then index.js of the very same folder you can do 在同一个文件夹中为不同的组件创建不同的文件,如StyledTabs.jsStyledTitle.js然后index.js你可以做同一个文件夹

export StyledTab from './StyledTab';
export StyleTitle from './StyledTitle';

This way your import { StyledTab } from 'path/to/StyledComponents' will work flawlessly 这样, import { StyledTab } from 'path/to/StyledComponents'将完美无瑕地运行

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

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