有没有总办法修改 antd 组件的默认参数

比如 Button 的默认属性 ghost 的值是 false,我想在我的项目中所有的 Button 的默认 ghost 都为 true

我现在的代码,所有的 Button 都要写上 ghost 属性

<Button ghost={true}></Button>

我想要实现,经过我简单的设置后,我在项目中使用 button 不需要使用 ghost 参数,也就是把原来的 Button 组件的默认参数篡改为 true

只需要:

<Button></Button>

不想要时

<Button ghost={false}></Button>
阅读 5.2k
3 个回答

那你自己再用高阶组件封装一个button不就完了吗?改他的代码肯定是不科学的,伪代码如下,你可以改成组件式的写法

import {Button} from 'antd'
function myButton(props = {}) {
    if(ghost in props) {
        return <Button ghost={props.ghost}>xxx</Button>
    }
    return <Button ghost={true}>xxx</Button>
}

myButton.js

import {Button} from 'antd'
export default props => {
  if(!(props && props.hasOwnProperty('ghost'))) {
    props = {...props, ghost: true}; 
  }
  return <Button {...props}>{props.children}</Button>
}

using

import Button from './myButton.js';
<Button>确定</Button>
新手上路,请多包涵

Import

import { Button} from 'ant-design-vue'
Button.props.ghost.default = true
Vue.use(Button)

Using

<a-button type="primary"> Primary </a-button>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进