曹筱君之在vue中创建button按钮插件

接着上面的讲,首先在自己的vue项目的src文件夹下的myUI文件夹中新建一个button文件夹,在里面新建一个index.vue,代码如下

<!---->
<template>
  <div class>
    <button
      :disabled="disabled"
      @click="click"
      :class="[
        color,
        size,
        {
          circle,
          round: !circle && round,
        },
      ]"
    >
      <!-- 加载中图标 -->
      <mu-loading :type="loading" v-if="loading > 0" />
      <!-- <loading :type="loading" v-if="loading > 0"> </loading> -->

      <!-- 字体图标 -->
      <i :class="['mu-iconfont', `mu-icon-${icon}`]" v-if="icon && loading < 1"></i>

      <!--  // 插槽 如果在按钮标签中间有标签,才会生成span标签-->
      <span v-if="$slots.default">
        <slot></slot>
      </span>
    </button>
  </div>
</template>

<script>
let colors = [
  "white",
  "red",
  "green",
  "blue",
  "black",
  "yellow",
  "orange",
  "gray",
];

let sizes = ["large", "emdium", "small", "mini"];
let icons = ["search", "success", "warn", "jiazai3"];
//首先得先下载这些图标
// import loading from "../loading/index.vue";
export default {
  // components: { loading },
  name: "mu-button",
  props: {
    disabled: Boolean,
    color: {
      type: String,
      default: "green",
      validator(value) {
        return colors.includes(value);
      },
    },

    // 是否圆形
    circle: Boolean,

    // 圆角
    round: Boolean,

    // 大小
    size: {
      type: String,
      default: "large",
      validator(value) {
        return sizes.includes(value);
      },
    },

    // 图标
    icon: {
      type: String,
      defaule: null,
      validator(value) {
        return icons.includes(value);
      },
    },
    // 是否加载
    loading: {
      type: Number,
      defaule: 0,
    },
  },
  data() {
    //这里存放数据
    return {
      s: "red",
    };
  },

  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    click(e) {
      this.$emit("click", e);
    },
  },

};
</script>
<style scoped lang='scss'>
@import "../variable.scss";

// 兄弟选择器 ,span前面要有i标签
i + span {
  margin-left: 5px;
}

// 圆形
.circle {
  border-radius: 50%;
  padding: 12px;
}

// 圆角
.round {
  border-radius: 20px;
}

button {
  border-radius: 3px;
  outline: none;
  border: none;
  padding: 10px 20px;
  line-height: 1;
  cursor: pointer;
}
@mixin background($color) {
  background: $color;
  color: $white;
  &:hover {
    background: mix($color, rgb(130, 130, 130));
  }
  &[disabled] {
    cursor: not-allowed;
    background: rgba($color: $color, $alpha: 0.5);
  }
}
.white{
  @include background($white);
  color:$black;
}
.green{
  @include background($green);
  // color:$black;
}
.red{
  @include background($red);
}
.yellow{
  @include background($yellow);
}
.gray{
  @include background($gray);
}
.blue{
  @include background($blue);
}
.black{
  @include background($black);
  color:$white;
}
.orange{
  @include background($orange);
}



// 属性选择器
// .green[disabled] {
//   cursor: not-allowed;
//   background: rgba($color: $green, $alpha: 0.5);
// }

// .green {
//   background: $green;
//   color: white;
// }

// // 鼠标移入
// .green:hover {
//   background: rgba($color: $green, $alpha: 0.5);
// }

// 大小
.large {
  font-size: $large;
}
.mini {
  font-size: $mini;
}
.small {
  font-size: $small;
}
.emdium {
  font-size: $emdium;
}
</style>

在button文件夹下新建index.js

import button from './index.vue'
button.install = function(vue){
    vue.component(button.name,button)
}
export default button;

在main.js中引入
import button from ‘./myUI/button/index.js’
Vue.use(button)

在组件中使用

在这里插入代码片<!--  -->
<template>
<div class=''>

<mu-loading :type="2"  color="red"></mu-loading> 
<mu-button color="green" size="large" round icon="warn" disabled :loading="0">啥也不是</mu-button>
<br>
<mu-button color="red" size="emdium" round  :loading="1">啥也不是</mu-button>
<br>
<mu-button color="yellow" size="small" icon="search" :loading="0">啥也不是</mu-button>
<br>
<mu-button color="blue" size="mini" icon="success" :loading="0">啥也不是</mu-button>
<br>
<mu-button color="white" size="mini" icon="success" circle :loading="0">啥也不是</mu-button>
<br>
<mu-button color="black" size="mini" icon="success" circle :loading="0"></mu-button>
<mu-button color="gray" size="mini"  circle :loading="0">啥</mu-button>
<mu-button color="orange" size="mini"  circle :loading="0">啥也不是</mu-button>
<mu-button color="green" size="mini"  round :loading="0">啥也不是ssss</mu-button>

</div>

</template>

<script>
// import load from "@/myUI/loading"
export default {
components: {
  // load,
},
data() {
//这里存放数据
return {

};
},
//监听属性 类似于data概念
computed: {},
//监控data中的数据变化
watch: {},
//方法集合
methods: {

},
//生命周期 - 创建完成(可以访问当前this实例)
created() {

},
//生命周期 - 挂载完成(可以访问DOM元素)
mounted() {

},
beforeCreate() {}, //生命周期 - 创建之前
beforeMount() {}, //生命周期 - 挂载之前
beforeUpdate() {}, //生命周期 - 更新之前
updated() {}, //生命周期 - 更新之后
beforeDestroy() {}, //生命周期 - 销毁之前
destroyed() {}, //生命周期 - 销毁完成
activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
}
</script>
<style scoped>

</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曹筱君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值