Web前端vue必做笔记之一:如何使用$attrs,inheritAttrs让你的组件可以更加灵活配置
组件MyInt.vue中
<template>
<div class="input-container">
<!-- $attrs保存了所有传过去的属性 -->
<input v-bind="$attrs" :value="value" @input="$emit('input')"/>
{{$attrs}}
</div>
</template>
<script>
export default {
props:['value'],
inheritAttrs:false //这个属性,可以禁止掉默认传过去属性会被添加到属性的根源上的行为
}
</script>
<style>
</style>
组件App.vue中
<template>
<div id="app">
<my-input v-model="message" type="text" placeholder="请输入用户名"></my-input>
<my-input v-model="message" type="password" placeholder="请输入密码"></my-input>
</div>
</template>
<script>
import MyInput from './components/MyInput'
export default {
components: {
MyInput
},
data() {
return {
message: 'hello world'
}
}
};
</script>
<style>
#app{
text-align: center;
}
</style>