en-switch.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <view class="box">
  3. <view class="input-box">
  4. <view class="input-box-left" :style="{'letter-spacing':labelWidth}">
  5. {{ label }}
  6. </view>
  7. <switch color="#3169FA" style="transform:scale(0.7)" :checked="true" @change="switchChange" />
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'en-switch',
  14. props: {
  15. type: {
  16. type: String,
  17. default: 'text'
  18. },
  19. label: {
  20. type: String,
  21. default: '状态'
  22. },
  23. value: {
  24. default: ''
  25. },
  26. trueValue: {
  27. default: 1
  28. },
  29. falseValue: {
  30. default: '0'
  31. }
  32. },
  33. data() {
  34. return {
  35. inputValue: false,
  36. labelWidth:0
  37. }
  38. },
  39. components: {},
  40. mounted() {
  41. this.setLabelWidth()
  42. this.setValue()
  43. },
  44. watch: {
  45. 'value': function () {
  46. this.inputValue = this.value
  47. }
  48. },
  49. methods: {
  50. setValue(){
  51. this.inputValue =this.value===this.trueValue
  52. },
  53. switchChange(e){
  54. if(e.detail.value){
  55. this.$emit('input', this.trueValue)
  56. }else {
  57. this.$emit('input', this.falseValue)
  58. }
  59. },
  60. setLabelWidth(){
  61. let differenceNum=4- this.label.length;
  62. if(differenceNum===2){
  63. this.labelWidth='2em'
  64. }else if(differenceNum===1){
  65. this.labelWidth='0.5em'
  66. }
  67. }
  68. }
  69. }
  70. </script>
  71. <style lang="scss" scoped>
  72. @import url("../../../static/css/en-common.css");
  73. .box{
  74. padding: 26rpx 0 24rpx 0;
  75. .input-box {
  76. display: flex;
  77. align-items: center;
  78. font-size: 32rpx;
  79. .input-box-left {
  80. color: #333333;
  81. width: 210rpx;
  82. //letter-spacing:1em;
  83. }
  84. }
  85. }
  86. </style>