Select组件二次封装 2020-06-22- 2021-10-29 前端 > vue > ant-design1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495/** * @Date: 2020-05-26 15:31 * @author jiabinbin * @Email 425605679@qq.com * @Description: */import S from 'ant-design-vue/es/select'import { isPromise } from '../../../utils/utils' // 判断是否是promiseimport { getDefaultProps } from '../../_utils' // 参见upload组件中的getDefaultPropsconst BSelect = { name: 'BSelect', props: Object.assign({}, S.props, { loadOptions: { // a-select-option数组对象 type: Array | Promise, required: true }, optionKey: { // selectOption的key type: String, default: 'id' }, optionValue: { // selectOption的value type: String, default: 'value' }, keyValue: { // 有时候需要展示值为 00-完成 01-未完成 02-待确定。开启之后 option的value和label同时展示 type: Boolean, default: false }, keyValueSplit: { // 开启keyValue之后,keyValue的连接符 type: String, default: '-' } }), model: { prop: 'value', event: 'change' }, data: () => ({ optionList: [] }), created () { this.loadData() }, watch: { loadOptions: { handler (c, n) { this.loadData() } }, immediate: true }, methods: { loadData () { if (isPromise(this.loadOptions)) { this.loadOptions.then(({ data }) => { this.optionList = data }) } else { this.optionList = this.loadOptions } }, renderOptions () { const { optionList } = this const { optionKey, optionValue, keyValue, keyValueSplit } = this return ( optionList.map(item => { const key = item[optionKey] const value = item[optionValue] const title = keyValue ? `${key} ${keyValueSplit} ${value}` : `${value}` return ( !keyValue ? ( <a-select-option key={key} title={value} value={key}>{value}</a-select-option> ) : ( <a-select-option key={key} title={title} value={key}>{key} {keyValueSplit} {value}</a-select-option> ) ) }) ) } }, render () { // props const props = getDefaultProps(S.props, this) const options = this.renderOptions() return ( <a-select {...{ props }} {...{ on: this.$listeners }} value={this.model}> {options} </a-select> ) }}export default BSelect Post author: 落小梅 | 月小芽Post link: https://yuexiaoya.com/2020/06/22/frontend/vue/ant-design/Select%E7%BB%84%E4%BB%B6%E4%BA%8C%E6%AC%A1%E5%B0%81%E8%A3%85/Copyright Notice: All articles in this blog are licensed under unless otherwise stated.