[Vue.js] 扩充元件(Component)

以 ElementUI 的 Button 为例

这是 ElementUI 的 Button
Origin Button
预设没有提供这样的样式
Full Button
该如何做到呢?

建立一个新元件,命名为 ElButtonFull.vue

ElButtonFull.vue

<template>    <el-button class="el-button-full" v-bind="$props">        <slot></slot>    </el-button></template><script>import { Button } from 'element-ui'export default {    name: 'ElButtonFull',    extends: Button};</script><style scoped>.el-button-full {    width: 100%;}</style>

透过设定 extends: ButtonElButtonFull 就会继承 Button 的 props 等所有属性
extends

<el-button> 中加上 v-bind="$props" 后,在 <el-button-full> 设定的 prop 会同步更新到子层 <el-button> 的 prop,就不需要手动对应每一个 prop 啦。

App.vue

<template>    ...    <el-button-full type="primary">Button</el-button-full>    ...</template><script>import ElButtonFull from 'components/ElButtonFull.vue'export default {    ...    components: {        ElButtonFull,        ...    }    ...}</script>

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章