何时使用
平铺型输入组件指的是组件直接渲染在属性名周围,不会有容器包裹。
源码展示
import { Component, Vue, Prop } from 'vue-property-decorator';
import { defaultNotEmpty } from '.';
import { isEqual } from 'lodash';
@Component
export class PropertyComponentBase extends Vue {
@Prop({ default: false })
disabled!: boolean;
@Prop()
defaultValue!: any;
@Prop()
value!: any;
@Prop()
maxLength!: number;
@Prop()
validError!: boolean;
@Prop()
title!: boolean;
@Prop()
getValueValidateErrorMessage!: Function;
@Prop()
isNotEmpty!: any;
//是否展示错误文案
@Prop()
showErrorText!: boolean;
@Prop()
emptyErrorText!: string;
//当前处于批量设计模式,批量设计下某些组件要更换渲染逻辑
@Prop()
batchMode!: boolean;
get errorText() {
if (!this.showErrorText) {
return '';
}
if (this.validError) {
return this.requiredErrorText || this.valuePatternErrorText;
} else {
return this.valuePatternErrorText;
}
}
get requiredErrorText() {
const emptyErrorText = this.emptyErrorText || `${this.title}不能为空`;
if (typeof this.isNotEmpty === 'function' && !this.isNotEmpty(this.value)) {
return emptyErrorText;
} else if (!this.isNotEmpty && !defaultNotEmpty(this.value)) {
return emptyErrorText;
}
return '';
}
get valuePatternErrorText() {
if (
typeof this.getValueValidateErrorMessage === 'function' &&
this.getValueValidateErrorMessage({ value: this.value })
) {
return this.getValueValidateErrorMessage({ value: this.value });
} else {
return '';
}
}
mounted() {
if (
this.defaultValue !== undefined &&
!isEqual(this.value, this.defaultValue)
) {
//初始值的赋值只有在默认值与当前值不相等时才能进行,不然可能引发一些无用的监听逻辑
if (this.isNotEmpty && typeof this.isNotEmpty === 'function') {
if (!this.isNotEmpty(this.value)) {
this.onValueChange(this.defaultValue);
}
} else if (!defaultNotEmpty(this.value)) {
this.onValueChange(this.defaultValue);
}
}
}
onValueChange(newVal) {
this.$emit('change', newVal);
}
//将数据保存在controller上,做缓存
saveCacheData(key, value) {
this.$emit('save', key, value);
}
otherValueChange(key, value) {
this.$emit('otherValueChange', key, value);
}
//触发一次初始化的propertyChange事件
initPropertyChange() {
this.$emit('initPropertyChange');
}
}属性说明
属性名 | 说明 | 属性值类型 |
disabled | 是否禁用,某些组件禁用态有不同的展示形式 | boolean |
defaultValue | 值为空时使用到的默认值 | any |
value | 值 | any |
maxLength | 最大长度 | number |
validError | 属性值校验状态 | boolean |
title | 属性标题 | string |
getValueValidateErrorMessage | 获取校验失败状态下的失败文案的函数 | function |
isNotEmpty | 判断值不为空的函数 | function |
showErrorText | 是否显示错误文案 | boolean |
emptyErrorText | 错误显示文案 | string |
batchMode | 是否开启批量模式,批量模式下组件展示可能发生变化 | boolean |
errorText | 属性校验错误文案 | string |
函数说明
函数名 | 说明 | 参数 |
onValueChange | 属性值改变,向外界传值函数 | 新的属性值 |
saveCacheValue | 将中间值保留在controller上,刷新会被清空 | 保存的key,保存的value |
otherValueChange | 非controller直接绑定的属性值改变,不会触发subscribtion | 目标属性key,新的目标属性值 |
initPropertyChange | 触发一次初始化的propertyChange事件 | 无 |
-
有用(6)
-
没有用(0)



