二次封装element-plus中的button
时间: 2023-10-13 14:12:36 浏览: 156
可以根据实际需要进行二次封装,例如:
```vue
<template>
<el-button v-bind="$attrs" v-on="$listeners" :type="type" :size="size" :disabled="disabled" :loading="loading">
<slot></slot>
</el-button>
</template>
<script>
import { ElButton } from 'element-plus';
export default {
name: 'MyButton',
components: {
ElButton
},
props: {
type: {
type: String,
default: 'primary'
},
size: {
type: String,
default: 'medium'
},
disabled: {
type: Boolean,
default: false
},
loading: {
type: Boolean,
default: false
}
}
}
</script>
```
这样就可以使用`<my-button>`代替`<el-button>`,并且可以自定义默认属性。例如:
```vue
<template>
<div>
<my-button @click="handleClick">Click me</my-button>
<my-button type="danger">Delete</my-button>
<my-button size="small" :loading="true">Loading</my-button>
<my-button disabled>Disabled</my-button>
</div>
</template>
<script>
import MyButton from './MyButton.vue';
export default {
components: {
MyButton
},
methods: {
handleClick() {
alert('Button clicked!');
}
}
}
</script>
```
阅读全文