【Antd】不能将类型“({ title: string; dataIndex: string; key: string; colSpan: number; width: string; align:

 代码如下

 const column = [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      colSpan: 0,
      width: '5%',
      align: 'center',
      render: (text: string, field: { name: string }, index: number) => index + 1,
    }];

 return <Table dataSource={dataSource} columns={column} pagination={false} bordered />;

错误提醒

重点:属性“align”的类型不兼容。不能将类型“string”分配给类型“AlignType | undefined”。

修改方案一

 {
      title: '序号',
      align: 'center' as 'center',
}

修改方案二

import React, { PureComponent } from "react";
import { Table } from "antd";

//引入culumns泛型
import { ColumnProps } from 'antd/es/table';

interface Iprops {
  name: string;
  age: number;
}

interface IState {
  count: number | string;
  value?:number;
}

// 定义列接口
const columns:ColumnProps<IUserInfo>[]= [
  {
    title: "名字 Name",
    width: 100,
    dataIndex: "name",
    key: "name",
    fixed: "left",
  },
  {
    title: "年龄",
    width: 100,
    dataIndex: "age",
    key: "age",
    fixed: "left",
  },
  { title: "地址", dataIndex: "address", key: "1" },
  { title: "地址2可以重复", dataIndex: "address", key: "2" },

  {
    title: "操作",
    key: "operation",
    fixed: "right",
    width: 100,
    render: () => <a href="#">action</a>,
  },
];


//行数据接口
interface IUserInfo {
  key:string;
  name:string;
  age:number;
  address:string
}

const data:IUserInfo[] = [
  {
    key: "1",
    name: "John Brown",
    age: 32,
    address: "New York Park",
  },
  {
    key: "2",
    name: "Jim Green",
    age: 40,
    address: "London Park",
  },
];

这样Table组件在使用columns就不会报错了

参考链接:react.js - typescript react项目中如果正确使用antd组件姿势,举个栗子:Table组件columns类型错误 - 个人文章 - SegmentFault 思否

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据您提供的引用内容,您遇到了antd-vue的select组件的一个错误,错误信息为“Invalid prop: type check failed for prop "value". Expected Object | String, got Number with value 0.”,这个错误是由于您传递给select组的value属性的类型不正确导致的。value属性的类型应该是Object或String,但您传递了一个Number类型的值0。 解决这个问题的方法是将传递给value属性的值转换为Object或String类型。如果您想将Number类型的值0转换为String类型,可以使用toString()方法。如果您想将Number类型的值0转换为Object类型,可以使用Object()方法。 以下是两种解决方法的示例代码: 1. 将Number类型的值0转换为String类型: ```html <template> <a-select v-model="selectedValue" :options="options" :value="selectedValue.toString()" /> </template> <script> export default { data() { return { selectedValue: 0, options: [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, ], }; }, }; </script> ``` 2. 将Number类型的值0转换为Object类型: ```html <template> <a-select v-model="selectedValue" :options="options" :value="selectedValueObject" /> </template> <script> export default { data() { return { selectedValue: 0, options: [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, ], }; }, computed: { selectedValueObject() { return { value: this.selectedValue.toString() }; }, }, }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值