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的使用。

最新文章

  1. phpexcel导入数据提示失败
  2. C#中的变量及命名规则
  3. 看大众点评V9新版如何为O2O止血 带领行业下半场回归理性
  4. [Unity3D]引擎崩溃、异常、警告、BUG与提示总结及解决方法
  5. jquery图片轮播,单张图片轮播时间不同
  6. 图片 文字 input等垂直居中对齐
  7. css应用四
  8. poj 2007 Scrambled Polygon 极角排序
  9. 百万行mysql数据库优化(补充)
  10. HUST 1353 Dartboard
  11. webAppbuilder微件使用教程1 快速入门
  12. Visio绘制用例图问题集锦
  13. Aspnet mvc移除WebFormViewEngine
  14. java-retry实现
  15. XML外部实体注入漏洞(XXE)
  16. Vuex详解
  17. git获取内核源码的方法
  18. SpringMVC之REST
  19. Oracle IF-ELSE 条件判断结构
  20. log4net按照不同的【LEVEL】级别输出到不同文件

热门文章

  1. 课堂实验-Bag
  2. Java的运行机制概括
  3. poj 2001 trie
  4. bzoj 3585: mex &amp;&amp; 3339: Rmq Problem -- 主席树
  5. mpdf与fpdf的使用比较
  6. paip.手机时间设置不能修改灰色禁用 解决大法
  7. Constant-on-time buck-boost regulator converts a positive input to a negative output
  8. Python中用MacFSEvents模块监视MacOS文件系统改变一例
  9. mysqlslap
  10. jdbcTemplate异常:like模糊查询报错(Parameter index out of range (1 &gt; number of parameters)