【问题标题】:Typescript Error for align in Material UI材质 UI 中对齐的打字稿错误
【发布时间】:2021-09-14 04:54:52
【问题描述】:
<Grid item xs={12} align="center">

给我

No overload matches this call.
  Overload 1 of 2, '(props: { component: ElementType<any>; } & Partial<Record<Breakpoint, boolean | GridSize>> & { alignContent?: GridContentAlignment | undefined; ... 7 more ...; zeroMinWidth?: boolean | undefined; } & CommonProps<...> & Pick<...>): Element', gave the following error.
    Property 'component' is missing in type '{ children: Element; item: true; xs: 12; align: string; }' but required in type '{ component: ElementType<any>; }'.
  Overload 2 of 2, '(props: DefaultComponentProps<GridTypeMap<{}, "div">>): Element', gave the following error.
    Type '{ children: Element; item: true; xs: 12; align: string; }' is not assignable to type 'IntrinsicAttributes & Partial<Record<Breakpoint, boolean | GridSize>> & { alignContent?: GridContentAlignment | undefined; ... 7 more ...; zeroMinWidth?: boolean | undefined; } & CommonProps<...> & Pick<...>'.
      Property 'align' does not exist on type 'IntrinsicAttributes & Partial<Record<Breakpoint, boolean | GridSize>> & { alignContent?: GridContentAlignment | undefined; ... 7 more ...; zeroMinWidth?: boolean | undefined; } & CommonProps<...> & Pick<...>'.ts(2769)
OverridableComponent.d.ts(17, 7): 'component' is declared here.
(JSX attribute) xs?: boolean | GridSize | undefined

请注意,我使用的是 tsx 顺便说一句,不知道为什么这会向我扔。我将文件扩展名更改为 jsx,一切正常(网格也来自材料 ui)

另外,问题出现在 `align="center" 部分,如果我删除不会出现错误

我也尝试将 align 更改为 alignItems 和 alignContent,从而解决了错误;但是它不会使内容与中心对齐

附带说明,不确定是否相关:在同一个文件中,我有一行:

<div style={{ align: "center" }}>

这也给我一个错误:

Type '{ align: string; }' is not assignable to type 'Properties<string | number, string & {}>'.
  Object literal may only specify known properties, and 'align' does not exist in type 'Properties<string | number, string & {}>'.ts(2322)
index.d.ts(1760, 9): The expected type comes from property 'style' which is declared here on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'

【问题讨论】:

    标签: css reactjs typescript material-ui


    【解决方案1】:

    Material UI 中没有像 align="center" 这样的属性。 Here 你可以读到有justify="center"alignItems="center"。您还可以在那里查看示例,以便将其应用到您的项目中。

    另外记得在&lt;Grid item&gt;上方使用&lt;Grid container&gt;才能管理它。 &lt;Grid container&gt; 也可以像&lt;Grid container item&gt; 一样在其中包含项目。我之前也遇到过同样的问题,希望对你有所帮助。

    这里有一个可行的解决方案 - LINK

    【讨论】:

    • 刚才这样做了,错误不再向我抛出,但项目没有与中心对齐。我还将 div 的样式更改为 alignItems 也只是为了采取好的措施。
    • 请分享更多代码,我无法仅通过 Grid 看到一行代码来帮助您。 codesandbox.io/s/material-ui-playground-vvwoj 这里有 react+material 操场,在此处添加这部分代码并发送链接,仅更改 App.jsx 文件
    • 所以你希望所有元素都在中心对吗?
    • 非常感谢!我找到了一种解决方法:通过将 xs={12} 更改为 xs="auto" 它也会出于某种原因将项目居中
    猜你喜欢
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    相关资源
    最近更新 更多