如果你不希望组件的根元素继承 attribute,你可以在组件的选项中设置 inheritAttrs: false。
上面是官网的解释
下面是我的理解:
<my-component message = "message" hhh = "hhh"></my-component>
Vue.component('my-component', {
inheritAttrs: true,
props: {
message: {
type: String,
required: true
},
},
template: '<div><h1 message = "message" v-bind="$attrs">测试</h1></div>'
})
这是一个自定义的组件,message是在props中注册过的属性,hhh没有在props中注册,用$attrs传递
当inheritAttrs: true时,根元素div继承了hhh属性:
当inheritAttrs: false时,根元素没有继承hhh属性: