JSwitch.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <template>
  2. <a-switch v-model="checkStatus" :disabled="disabled" @change="handleChange"/>
  3. </template>
  4. <script>
  5. export default {
  6. name: 'JSwitch',
  7. props: {
  8. value:{
  9. type: String,
  10. required: false
  11. },
  12. disabled:{
  13. type: Boolean,
  14. required: false,
  15. default: false
  16. },
  17. options:{
  18. type:Array,
  19. required:false,
  20. default:()=>['Y','N']
  21. }
  22. },
  23. data () {
  24. return {
  25. checkStatus: false
  26. }
  27. },
  28. watch: {
  29. value:{
  30. immediate: true,
  31. handler(val){
  32. if(!val){
  33. this.checkStatus = false
  34. this.$emit('change', this.options[1]);
  35. }else{
  36. if(this.options[0]==val){
  37. this.checkStatus = true
  38. }else{
  39. this.checkStatus = false
  40. }
  41. }
  42. }
  43. }
  44. },
  45. methods: {
  46. handleChange(checked){
  47. let flag = checked===false?this.options[1]:this.options[0];
  48. this.$emit('change', flag);
  49. }
  50. },
  51. model: {
  52. prop: 'value',
  53. event: 'change'
  54. }
  55. }
  56. </script>