【问题标题】:How to use AG-Grid in React? I am getting Build error如何在 React 中使用 AG-Grid?我收到构建错误
【发布时间】:2020-03-11 18:46:43
【问题描述】:

下面是组件文件:

import { AgGridReact } from '@ag-grid-community/react';
import {AllCommunityModules} from '@ag-grid-community/all-modules';
import '@ag-grid-community/all-modules/dist/styles/ag-grid.css';
import '@ag-grid-community/all-modules/dist/styles/ag-theme-balham.css';



export default class FormGrid extends React.Component<IFormGridProps,IFormGridState> {
   constructor(props: IFormGridProps, state: IFormGridState){
   super(props);
   this.state = {
     columnDefs: [
      { headerName: "Make", field: "make" },
      { headerName: "Model", field: "model" },
      { headerName: "Price", field: "price" }],
     rowData: [
      { make: "Toyota", model: "Celica", price: 35000 },
      { make: "Ford", model: "Mondeo", price: 32000 },
      { make: "Porsche", model: "Boxter", price: 72000 }]
     }
    }
public render(): React.ReactElement<IFormGridProps> {
return (
  <div className={ styles.formGrid }>
    <div className={ styles.container }>
      <div className={ styles.row }>
      <div className="ag-theme-balham" style={ {height: '200px', width: '600px'} }>
      <AgGridReact
        columnDefs={this.state.columnDefs}
        rowData={this.state.rowData}
        modules={AllCommunityModules}>
      </AgGridReact>
      </div>  
      </div>
    </div>
  </div>
 );
 }
 }

错误如下:

错误 TS2339:“IntrinsicAttributes & IntrinsicClassAttributes & Readonly...'

IFormGridState.ts 文件如下:

export interface IFormGridState{
   columnDefs: any,
   rowData: any
}  

我不明白为什么会出现错误。请帮我。

【问题讨论】:

    标签: reactjs ag-grid spfx


    【解决方案1】:

    确保您已安装“@ag-grid-community/all-modules”软件包 我试过你的代码,它编译得很好,但我必须安装上面的包。你也可以粘贴你的导入吗?

    附加说明/提示:

    • 在第 2 行,您只能将 props 作为 React 类组件的参数传递(但不会给您这样的错误)
    • 无需为您的渲染方法添加类型

    【讨论】:

    • ag-grid 模块已安装。我在问题中添加了导入
    • 你从哪里得到 styles.formGrid 和类似的?如果不是这种情况,请尝试逐步调试。首先确保除了 ` ` 部分之外的所有部分都已编译。所以只是暂时删除它。然后如果上面的工作正常,然后用空数组替换状态中的变量,如下所示:` `
    猜你喜欢
    • 2021-03-27
    • 2019-10-28
    • 2018-02-20
    • 2020-03-31
    • 2020-11-06
    • 2016-05-06
    • 2020-07-04
    • 2021-09-13
    • 2019-09-08
    相关资源
    最近更新 更多