vue vm.$attrs 使用
2024-08-27 09:01:34
1、vm.$attrs 说明
https://cn.vuejs.org/v2/api/#vm-attrs
将父组件的属性(除去在props中传入的属性)传递给子组件。
2、代码分析
以下是element-ui input源码
<input
:tabindex="tabindex"
v-if="type !== 'textarea'"
class="el-input__inner"
v-bind="$attrs"
:type="type"
:disabled="inputDisabled"
:readonly="readonly"
:autocomplete="autoComplete || autocomplete"
:value="currentValue"
ref="input"
@compositionstart="handleComposition"
@compositionupdate="handleComposition"
@compositionend="handleComposition"
@input="handleInput"
@focus="handleFocus"
@blur="handleBlur"
@change="handleChange"
:aria-label="label"
>
el-input的props源码:
props: {
value: [String, Number],
size: String,
resize: String,
form: String,
disabled: Boolean,
readonly: Boolean,
type: {
type: String,
default: 'text'
},
autosize: {
type: [Boolean, Object],
default: false
},
autocomplete: {
type: String,
default: 'off'
},
/** @Deprecated in next major version */
autoComplete: {
type: String,
validator(val) {
process.env.NODE_ENV !== 'production' &&
console.warn('[Element Warn][Input]\'auto-complete\' property will be deprecated in next major version. please use \'autocomplete\' instead.');
return true;
}
},
validateEvent: {
type: Boolean,
default: true
},
suffixIcon: String,
prefixIcon: String,
label: String,
clearable: {
type: Boolean,
default: false
},
tabindex: String
},
以下是el-input小例子:
<el-input maxlength="5" minlength="2">
</el-input>
<el-input>
组件添加了2个原生属性,注意这2个原生属性并没有在prop里面,这2个属性是控制input的最大输入和最小输入长度的,那么这2个属性现在仅仅放在了父元素<el-input>
上,如何将其传递给素<el-input>
内的原生input子元素呢?不传递则这2个属性不起作用,因为子input上没有这2个属性。答案就是通过v-bind="$attrs"
来实现,它将父元素所有非prop的特性都绑定在了子元素input上,否则还得在props里声明maxlength,minlength,代码量增大。这就是
$attrs
的使用。最新文章
- phpexcel导入数据提示失败
- C#中的变量及命名规则
- 看大众点评V9新版如何为O2O止血 带领行业下半场回归理性
- [Unity3D]引擎崩溃、异常、警告、BUG与提示总结及解决方法
- jquery图片轮播,单张图片轮播时间不同
- 图片 文字 input等垂直居中对齐
- css应用四
- poj 2007 Scrambled Polygon 极角排序
- 百万行mysql数据库优化(补充)
- HUST 1353 Dartboard
- webAppbuilder微件使用教程1 快速入门
- Visio绘制用例图问题集锦
- Aspnet mvc移除WebFormViewEngine
- java-retry实现
- XML外部实体注入漏洞(XXE)
- Vuex详解
- git获取内核源码的方法
- SpringMVC之REST
- Oracle IF-ELSE 条件判断结构
- log4net按照不同的【LEVEL】级别输出到不同文件
热门文章
- 课堂实验-Bag
- Java的运行机制概括
- poj 2001 trie
- bzoj 3585: mex &;&; 3339: Rmq Problem -- 主席树
- mpdf与fpdf的使用比较
- paip.手机时间设置不能修改灰色禁用 解决大法
- Constant-on-time buck-boost regulator converts a positive input to a negative output
- Python中用MacFSEvents模块监视MacOS文件系统改变一例
- mysqlslap
- jdbcTemplate异常:like模糊查询报错(Parameter index out of range (1 >; number of parameters)