How can I dynamically set the default value when use defineProps
Api
#13658
Unanswered
ngd-b
asked this question in
Help/Questions
Replies: 1 comment 1 reply
-
You can use a factory function for the default, which can call withDefaults(defineProps<{ foo?: number }>(), {
foo() {
return inject('defaultFoo', 3)
}
}) I don't think there's a way to set all the defaults in one go, you'd need to have a separate function for each prop. e.g.: const props = withDefaults(defineProps<TextOptions & { prop: TextType }>(), {
prop: "textStyle",
foo: () => useText().defaultTextProps.foo,
bar: () => useText().defaultTextProps.bar
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Since unpassed
bool
type fields in vue are set tofalse
by default. It is necessary to set default values forbool
type fields.However, I have encapsulated a public component, and the default values will be different. I cannot determine in the parent component whether it is a value passed in by the user or vue's own default value, So provide the default value through
provide/inject
initializaion and initialize it dynamically in the component,but it reports an error when accessing the page. there is no error prompt in the code.
How should I deal with it.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions