0

我想创建一个组件

const StyledDiv = styled.div`
width: 10rem;
height: 3rem;
border-radius: 0.2rem;
background-color: ${({ theme, colorVariant, colorType }) => theme.colors[colorType]. 
 [colorVariant]}
`;

但是显示错误

'Pick<DetailedHTMLProps<HTMLAttributes, HTMLDivElement>, "slot" 类型上不存在属性 'colorVariant' | “风格” | “标题” | ... 252 更多... | "css"> & { ...; } & ThemeProps<...>'。

这是我设置的主题

export default {
  'colors' : {
    'blue' : {
      '100' : '#232f3e',
      '200' : '#233d51',
      '300' : '#006170',
      '400' : '#008296',
      '450' : '#008091',
      '500' : '#d4e4e6',
      '600' : '#e5f2f3',
    },
    'grey' : {
      '600' : '#8ca1a3',
      '700' : '#768283',
      '800' : '#e7e9e9',
      '900' : '#ffffff',
    },
    'red' : {
      '1000' : '#b40909',
    },
    'green' : {
      '1100' : '#417505',
    },
    'yellow' : {
      '1200' : '#f5a623',
    }
  }
};
4

2 回答 2

2

你在用styled-components吗?如果是这样,似乎 div 方法也将参数作为您需要的扩展道具。这就是我的意思:

const StyledDiv = styled.div<{ colorVariant: string, colorType: string }>`
  width: 10rem;
  height: 3rem;
  border-radius: 0.2rem;
  background-color: ${({ theme, colorVariant, colorType }) => theme.colors[colorType]. 
   [colorVariant]}
`;
于 2020-07-27T07:16:12.587 回答
1

我发现的一种方法是


interface IStyledColorBar {
  colorVariant: string,
  colorType: string,
};

type StyledProps<T = {}> = React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement> & Partial<T>, HTMLDivElement>;

const StyledDiv = styled.div <StyledProps <IStyledColorBar>>`
  width: 10rem;
  height: 3rem;
  border-radius: 0.2rem;
  background-color: ${({ theme, colorVariant, colorType }) => theme.colors[colorType][colorVariant]}
`;

我从 https://github.com/emotion-js/emotion/issues/802#issuecomment-444181912得到了这个解决方案

于 2020-07-27T07:09:15.383 回答