美文网首页VUE
ViewUI Icon组件的vue3改造

ViewUI Icon组件的vue3改造

作者: EasyNetCN | 来源:发表于2020-11-22 09:45 被阅读0次

    为了学习和应用vue3,我们开始了把viewui(https://github.com/view-design/ViewUI)基于vue3的重构,下面是其中Icon的组件示例:

    <template>
      <i :class="classes" :style="styles" @click="handleClick"></i>
    </template>
    <script>
    import { computed, toRefs } from "vue";
    
    const prefixCls = "ivu-icon";
    
    export default {
      name: "Icon",
      props: {
        type: {
          type: String,
          default: "",
        },
        size: [Number, String],
        color: String,
        custom: {
          type: String,
          default: "",
        },
      },
      emits: ["click"],
      setup(props, ctx) {
        const { type, size, color, custom } = toRefs(props);
    
        const classes = computed(() => {
          return [
            `${prefixCls}`,
            {
              [`${prefixCls}-${type.value}`]: type.value !== "",
              [`${custom.value}`]: custom.value !== "",
            },
          ];
        });
    
        const styles = computed(() => {
          let style = {};
    
          if (size) {
            style["font-size"] = `${size.value}px`;
          }
    
          if (color) {
            style.color = color.value;
          }
    
          return style;
        });
    
        const handleClick = (evt) => {
          ctx.emit("click", evt);
        };
    
        return {
          handleClick,
          classes,
          styles,
        };
      },
    };
    </script>
    

    相关文章

      网友评论

        本文标题:ViewUI Icon组件的vue3改造

        本文链接:https://www.haomeiwen.com/subject/rgoyiktx.html