简体   繁体   English

React - 无法从 index.js 导出多个文件

[英]React - cannot export multiple files from index.js

I am creating a styled-components with Atomic Design and I want to separate styled files into one folder and have one index.js to export all files.我正在使用 Atomic Design 创建一个样式组件,我想将样式文件分离到一个文件夹中,并有一个 index.js 来导出所有文件。 I have found that can be done like this way: Splitting Styled-Components into multiple files but export them as one file but In my case when I want to export file inside index.js我发现可以这样完成:将样式组件拆分为多个文件,但将它们导出为一个文件,但在我的情况下,当我想在index.js导出文件时

export StyledClosestMeet from 'styledComponents/StyledClosestMeet.js';

I have error我有错误

Declaration or statement expected.ts(1128)声明或声明 expected.ts(1128)

Inside styledComponents folder I have second file:在 styledComponents 文件夹中,我有第二个文件:

import styled from 'styled-components';
import variables from 'settings/variables';

const StyledClosestMeet = styled.div`
  color: ${variables.colorMain};
  font-size: 2rem;
  font-weight: bold;
  text-transform: uppercase;
`;

And later I want to import that file inside Atom file:后来我想在 Atom 文件中导入该文件:

import React from 'react';
import {StyledClosestMeet} from 'styledComponents/StyledClosestMeet';

const ClosestMeet = () => {
  return <StyledClosestMeet>Najbliższe spotkanie</StyledClosestMeet>;
};

export default ClosestMeet;

I am using jsconfig "baseUrl": "./src"我正在使用 jsconfig "baseUrl": "./src"

You can create an index.js file to export both files like this:您可以创建一个 index.js 文件来导出这两个文件,如下所示:

export { default as Styled1 } from "./styled1";
export { default as Styled2 } from "./styled2";

and then import them like this:然后像这样导入它们:

import { Styled1, Styled2 } from "./styled/index";

Check out this example看看这个例子

You've to export StyledClosestMeet你必须导出 StyledClosestMeet

import styled from 'styled-components';
import variables from 'settings/variables';

const StyledClosestMeet = styled.div`
  color: ${variables.colorMain};
  font-size: 2rem;
  font-weight: bold;
  text-transform: uppercase;
`;

export { StyledClosestMeet }

And you can export StyledClosestMeet like below.您可以像下面这样导出 StyledClosestMeet。

export { StyledClosestMeet } from 'styledComponents/StyledClosestMeet.js';

暂无
暂无

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

相关问题 出现“错误:插件/预设文件不允许导出对象,只能导出功能。” 来自 babel-preset-react-app/index.js - Getting "Error: Plugin/Preset files are not allowed to export objects, only functions." from babel-preset-react-app/index.js 组合多个“import * as<name> 从 '<path> ' 其中包含多个 export const 到 index.js 的 1 个导出中</path></name> - Combine multiple "import * as <name> from '<path>' which contains multiple export const into 1 export for index.js 如何在 react naitve 中从一个 index.js 文件中使用 require() 导出图像路径? - How can I export images path with require() from one index.js file in react naitve? 如何在 index.js 中重新导出多个命名导出和多个默认导出? - How to Re-Export Multiple Named Export and Multiple Default Export in index.js? 无法将脚本从index.js复制到React Native中的资产文件夹 - Cannot copy scripts from index.js to assets folder in react native React从同一index.js的不同HTML页面渲染多个DOM元素 - React render multiple DOM elements from different HTML pages from same index.js index.js 文件如何在 React 组件目录中工作? - How do index.js files work in React component directories? 如何导出 index.js 中的所有组件? - How to export all components in index.js? 在 index.js 中重命名导出默认值 - rename export default in index.js ESM 从 index.js 无法正常工作的文件夹中导入所有文件 - ESM importing all files from folder with index.js not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM