Skip to content

如何减少重复代码,以面向对象方式开发? #6837

Discussion options

You must be logged in to vote
function useBoolean(defaultValue = false) {
  const bool = ref(defaultValue);

  const toggle = (nextValue = !bool.value) => {
    bool.value = nextValue;
  }

  return [bool, toggle];
}

function useDialog() {
  const [visible, toggle] = useBoolean();

  const open () => toggle(true);
  const close () => toggle(false);

  return { visible, open, close }
}

function useFormDialog() {
  const dialog = useDialog();

  const model = reactive({
    name: '',
  });

  const submit = () => {
    // form validation...
    dialog.close();
  }

  return { model, ...dialog, submit }
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@shuxinqin
Comment options

Answer selected by shuxinqin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants