Button.vue 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <template>
  2. <!-- use plane -->
  3. <!-- 传入bgColor改变按钮背景色 -->
  4. <!-- state切换button的状态 调用cancel()可以切换 -->
  5. <!-- text为按钮文字 -->
  6. <div class="container">
  7. <button
  8. :disabled="state"
  9. class="confirm"
  10. :style="{ background: btnData.bgColor }"
  11. @click="confirm"
  12. >
  13. {{ text }}
  14. </button>
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'Button',
  20. props: {
  21. btnData: {
  22. type: Object,
  23. default: () => ({ text: '确认' })
  24. }
  25. },
  26. data() {
  27. return {
  28. text: this.btnData.text,
  29. state: false
  30. }
  31. },
  32. methods: {
  33. confirm() {
  34. // 这里是激活父组件的事件,因为子组件是不会冒泡到父组件上的,必须手动调用$emit
  35. // 相对应父组件要在调用该组件的时候,将其挂载到上面
  36. this.$emit('confirm')
  37. }
  38. }
  39. }
  40. </script>
  41. <style scoped>
  42. .confirm {
  43. border: none;
  44. color: #fff;
  45. width: 100%;
  46. padding: 1rem 0;
  47. border-radius: 4px;
  48. font-size: 1.6rem;
  49. background: #5da1fd;
  50. }
  51. </style>