Skip to content

Instantly share code, notes, and snippets.

@okyungjin
Created May 4, 2022 04:49
Show Gist options
  • Save okyungjin/12109650c1020dabc683ee13ec466195 to your computer and use it in GitHub Desktop.
Save okyungjin/12109650c1020dabc683ee13ec466195 to your computer and use it in GitHub Desktop.
[Vue 3] Set default value to props
// 1. destrcuting 사용
const {
label = 'Normal Button',
borderRadius,
borderColor = 'transparent',
backgroundColor = '#FF4800',
} = defineProps<Props>();
// 2. withDefaults 사용
const props = withDefaults(defineProps<Props>(), {
label: 'Normal Button',
borderColor: 'transparent',
backgroundColor: '#FF4800',
});
@okyungjin
Copy link
Author

1번 방법은 사용하면 안 된다. label이라는 key가 없을 때 기본 값이 설정되지 않기 때문!

const { label, borderRadius, borderColor, backgroundColor } = withDefaults(
  defineProps<Props>(),
  {
    label: 'Normal Button',
    borderColor: 'transparent',
    backgroundColor: '#FF4800',
  }
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment