如何在反应中导入自定义样式组件

How can I import custom styled-components in react

我在 styled-ui 文件夹中有 Form.js 个文件。

Form.js

// input
export const FormControl = styled.input`
  border-radius: 3px;
  border: 1px solid #b2bec3;
  padding: 0 10px;
  height: 34px;
  font-size: 15px;
  outline: 0;
  width: 100%;
`

然后我像这样在 styled-ui/index.js 中导入 Form.js 文件。

import * as Form from './Form'
import * as Section from './Section'

export default {
  ...Form,
  ...Section
}

这是我的 conatiner 文件,其中导入样式组件

import {
  FormControl
} from '@/styled-ui'

然后当我使用这个时,

<FormControl></FormControl>

我收到错误 '@/styled-ui' does not contain an export named 'FormControl'. 但是,当我这样使用时效果很好。

import styled from '@/styled-ui'
`<styled.FormControl></styled.FormControl>`

我正在使用 webpack 进行开发和生产

{
            test: /\.css$/,
            use: [
              require.resolve('style-loader'),
              {
                loader: require.resolve('css-loader'),
                options: {
                  importLoaders: 1,
                },
              },
              {
                loader: require.resolve('postcss-loader'),
                options: {
                  // Necessary for external CSS imports to work
                  // https://github.com/facebookincubator/create-react-app/issues/2677
                  ident: 'postcss',
                  plugins: () => [
                    require('postcss-flexbugs-fixes'),
                    autoprefixer({
                      browsers: [
                        '>1%',
                        'last 4 versions',
                        'Firefox ESR',
                        'not ie < 9', // React doesn't support IE8 anyway
                      ],
                      flexbox: 'no-2009',
                    }),
                  ],
                },
              },
            ],
          },

我现在很困惑。请告诉我如何解决这个问题?

import * as Form from './Form'

...

export default {
  ...Form,
  ...Section
}

在你的情况下,和写作一样

import * as Form from './Form'

...

export default {
  FormControl: Form.FormControl,
  ...Section
}

这就是 <styled.FormControl> 工作正常的原因。

您可以在 index.js 文件中执行类似

的操作
export * from './Form'