網站首頁 編程語言 正文
在一個項目開發時,經常用到input組件或者textarea組件,當前項目使用的時antdv@1.7.8。提交測試時,測試人員反饋AInput和ATextarea組件沒有限制字符長度導致保存時數據庫插入報錯,因此需要在前端需要限制字符長度。由于項目已經開發過半,因此想通過使用設置默認值方式設置maxLength屬性。
確定方案
- 普通input最大長度默認設置為64
- textarea最大長度設置為200
最終實現
再main.js文件中增加代碼
import Antd, { Input } from "ant-design-vue";
Input.props.maxLength.default = 64;
然后手動再將每個ATextarea單獨增加:max-length="200"
踩坑過程
- 開始想通過修改默認值的方式分別修改AInput、ATextarea兩類組件,代碼如下:
src\main.js
import Antd, { Input } from "ant-design-vue";
Input.props.maxLength.default = 64;
Input.TextArea.props.maxLength.default = 200;
-
運行結果
結果發現AInput和ATextarea組件的maxlength都變成了200 -
原因查找
\node_modules\ant-design-vue\es\input\TextArea.js
使用函數式組件編寫,核心代碼如下
import inputProps from './inputProps';
var TextAreaProps = _extends({}, inputProps, {
autosize: PropTypes.oneOfType([Object, Boolean]),
autoSize: PropTypes.oneOfType([Object, Boolean])
});
export default {
name: 'ATextarea',
inheritAttrs: false,
props: _extends({}, TextAreaProps),
render: function render() {
var props = {
props: _extends({}, getOptionProps(this), {
prefixCls: prefixCls,
inputType: 'text',
value: fixControlledValue(stateValue),
element: this.renderTextArea(prefixCls),
handleReset: this.handleReset
}),
on: getListeners(this)
};
return h(ClearableLabeledInput, props);
}
}
ATextarea組件的props屬性使用的_extends({}, TextAreaProps)
進行淺拷貝
\node_modules\ant-design-vue\es\input\Input.js
也使用函數式組件編寫,代碼如下:
import inputProps from './inputProps';
export default {
name: 'AInput',
inheritAttrs: false,
props: _extends({}, inputProps),
render: function render() {
var props = {
props: _extends({}, getOptionProps(this), {
}),
on: getListeners(this)
};
return h(ClearableLabeledInput, props);
}
};
同樣AInput組件的props屬性也使用的_extends({}, inputProps)
進行淺拷貝
node_modules\ant-design-vue\es\input\inputProps.js
定義了props屬性
import PropTypes from '../_util/vue-types';
export default {
maxLength: PropTypes.number,
};
結論:PropTypes.number
是引用類型,因此被淺拷貝后實際都是一個地址。無論給AInput還是ATextarea的maxlength賦默認值最終是相互影響的,所以導致上邊的問題。
node_modules\ant-design-vue\es\_util\vue-types\index.js
定義vue類型的公共代碼
var VuePropTypes = {
get number() {
return toType('number', {
type: Number
}).def(currentDefaults.number);
},
}
node_modules\ant-design-vue\es\_util\vue-types\utils.js
定義vue類型的公共代碼
/**
* Adds `isRequired` and `def` modifiers to an object
* @param {string} name - Type internal name
* @param {object} obj - Object to enhance
* @returns {object}
*/
export var toType = function toType(name, obj) {
Object.defineProperty(obj, '_vueTypes_name', {
enumerable: false,
writable: false,
value: name
});
withRequired(obj);
withDefault(obj);//定義默認值,實際上為obj添加了一個def方法,通過該方法定義默認值
if (isFunction(obj.validator)) {
obj.validator = obj.validator.bind(obj);
}
return obj;
};
/**
* Adds a `def` method to the object returning a new object with passed in argument as `default` property
* @param {object} type - Object to enhance
*/
export var withDefault = function withDefault(type) {
Object.defineProperty(type, 'def', {
value: function value(def) {
if (def === undefined && this['default'] === undefined) {
this['default'] = undefined;
return this;
}
if (!isFunction(def) && !validateType(this, def)) {
warn(this._vueTypes_name + ' - invalid default value: "' + def + '"', def);
return this;
}
this['default'] = isArray(def) || isPlainObject(def) ? function () {
return def;
} : def;
return this;
},
enumerable: false,
writable: false
});
};
原文鏈接:https://blog.csdn.net/lianlin21212411/article/details/132162594
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-07-04 圖神經網絡GNN算法基本原理詳解_python
- 2022-04-08 c++11中std::move函數的使用_C 語言
- 2022-10-31 Linux系統docker部署.net?core3.1的詳細步驟_docker
- 2022-07-13 Atom 常用快捷鍵
- 2023-10-16 清理linux日志
- 2022-11-26 React常見跨窗口通信方式實例詳解_React
- 2022-08-22 GoFrame實現順序性校驗示例詳解_Golang
- 2022-07-10 oracle中的session
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支