vue官网是这样介绍的:
包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class
和 style
除外)。当一个组件没有声明任何 prop 时,这里会包含所有父作用域的绑定 (class
和 style
除外),并且可以通过 v-bind="$attrs"
传入内部组件——在创建高级别的组件时非常有用。
<div id="app"> A{{msg}} <my-button :msg="msg"></my-button> </div>
<script> let vm = new Vue({ el: ‘#app‘, data: { msg: ‘100‘ }, components: { ‘MyButton‘: { props: [‘msg‘], template: `<div>B<my-input :msg="msg"></my-input></div>`, components: { ‘MyInput‘: { props: [‘msg‘], template: ‘<div>C{{msg}}</div>‘ } } }, } }) </script>
<script> let vm = new Vue({ el: ‘#app‘, data: { msg: ‘100‘ }, components: { ‘MyButton‘: { // props: [‘msg‘], template: `<div>B<my-input v-bind="$attrs"></my-input></div>`, components: { ‘MyInput‘: { props: [‘msg‘], template: ‘<div>C{{msg}}</div>‘ } } }, } }) </script>
注意组件别写错了,组件是在父级模板中使用的,最外层的也是模板,el表示模板挂载在哪个元素的位置
原文:https://www.cnblogs.com/wuxianqiang/p/10452662.html