簡單的vue按鈕組件

點擊確定之後更改狀態
button.vue

<template>
    <div class="container">
        <button
        v-if='state'
        @click='confirm'
        class='confirm'
        :style="{background:btnColor}"
        >
            <slot>按鈕</slot>
        </button>
        <button class="confirm" :disabled='!state' v-else>正在加載</button>
    </div>
</template>

<script>
export default{
    data(){
        return {
            state:true,
            btnColor:'red'
        }
    },
    props:{
        text:{
            type:String,
            default:'註冊'
        }
    },
    methods:{
        confirm(){
            this.state=false;
            this.btnColor = 'blue';
            this.$emit("confirm");
        },
        cancel(){
            let that=this;
            setTimeout(function(){
                  that.state = true;
                  that.btnColor = 'red';
            }, 1000)
        }
    }
}
</script>
<style scoped>
.confirm {
  border: none;
  color: #fff;
  width: 100%;
  padding: 1rem 0;
  border-radius: 4px;
  font-size: 1.6rem;
  background: #5da1fd;
}
</style>

index.vue

<template>
    <Btn @confirm="confirm" ref="btn">確定</Btn>
</template>

<script>
import Btn from './components/button'
export default {
  components: {
    Btn
  },
  methods: {
    confirm(){
        this.$refs.btn.cancel()
    }
  }
}
</script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章