demo.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <template>
  2. <div class="demo">
  3. <h2>基础用法</h2>
  4. <nut-cell class="switch-adjust">
  5. {{ text }}
  6. <nut-switch
  7. @switch-change="switchChange"
  8. class="switch-decoration"
  9. ></nut-switch>
  10. </nut-cell>
  11. <h2>change事件</h2>
  12. <nut-cell class="switch-adjust">
  13. chane
  14. <nut-switch
  15. @switch-change="change"
  16. class="switch-decoration"
  17. ></nut-switch>
  18. </nut-cell>
  19. <h2>自定义颜色</h2>
  20. <nut-cell class="switch-adjust">
  21. color
  22. <nut-switch activeColor="blue" class="switch-decoration"></nut-switch>
  23. </nut-cell>
  24. </div>
  25. </template>
  26. <script lang="ts">
  27. import { toRefs, reactive } from 'vue';
  28. import { createComponent } from '@/utils/create';
  29. import { isObject } from '@vue/shared';
  30. const { createDemo } = createComponent('switch');
  31. export default createDemo({
  32. props: {},
  33. setup() {
  34. const response = reactive({
  35. text: '开'
  36. });
  37. const switchChange = (event: Event, isOpen: boolean) => {
  38. response.text = isOpen ? '开' : '关';
  39. };
  40. const change = (event: Event, isOpen: boolean) => {
  41. console.log('触发了change事件,开关状态:', isOpen);
  42. };
  43. return {
  44. ...toRefs(response),
  45. switchChange,
  46. change
  47. };
  48. }
  49. });
  50. </script>
  51. <style lang="scss" scoped>
  52. .switch-adjust {
  53. font-size: 18px;
  54. color: rgba(102, 102, 102, 1);
  55. justify-content: space-between;
  56. align-items: center;
  57. }
  58. .switch-decoration {
  59. cursor: pointer;
  60. }
  61. </style>