defineOptions
#430
Replies: 21 comments 40 replies
-
It's nice you even made a plugin for it! |
Beta Was this translation helpful? Give feedback.
-
It is a really nice enhancement! Maybe the RFC can be written in a more formal format that looks like THE ONE rfc ? Also some quick questions. Do we need to handle conflicts in defineOptions and a regular script block? Or conflicts with props? e.g. <script>
export default {
name: 'Component1' // conflict in plain script
}
</script>
<script setup>
defineOptions({
name: 'Component2',
props: ['prop1']
})
const props = defineProps({
prop2: String // conflict in props
})
</script> |
Beta Was this translation helpful? Give feedback.
-
I think it's doable, but would require some lexical considerations. For example, this is obviously invalid: import { ref } from 'vue'
const source = ref(null)
defineOptions({
beforeRouteEnter (to, from) {
source.value = from
return true
}
}) But it's more debatable if the following should be valid: import { ref } from 'vue'
const message = "Hello, world!"
defineOptions({
beforeRouteEnter (to, from) {
console.log(message)
return true
}
})
const isOpen = ref(false) Perhaps it should have different meaning if |
Beta Was this translation helpful? Give feedback.
-
In general, I think
defineOptions({ props: { foo: string }})
defineProps({ foo: Number }) |
Beta Was this translation helpful? Give feedback.
-
I updated to a more formal RFC. |
Beta Was this translation helpful? Give feedback.
-
I like this, but I think it should be limited to common options that can't be done any other way -- |
Beta Was this translation helpful? Give feedback.
-
Another usage scenario: with this feature, you can use JSX in script-setup. <script setup lang="tsx">
defineOptions({
render() {
return <h1>Hello World</h1>
}
})
</script> |
Beta Was this translation helpful? Give feedback.
-
https://vuejs.org/guide/extras/composition-api-faq.html#does-composition-api-cover-all-use-cases
I don't quite understand this passage. |
Beta Was this translation helpful? Give feedback.
-
I like how well-thought-out this proposal is. Some of my concerns are:
Maybe we can make this additive instead - i.e. restrict |
Beta Was this translation helpful? Give feedback.
-
The more I think about it, the more I think it can be confusing to expose options within the setup because there is not direct relationship between the scope of setup and the scope of options so that would result in some weird things when people try to use variables in defineOptions, unless we make it very explicit that no reference is ever allowed within and checking it on compilation |
Beta Was this translation helpful? Give feedback.
-
Expect this feature, Thanks to the Vue team |
Beta Was this translation helpful? Give feedback.
-
Perhaps we could be a little more granular and introduce single responsibility functions like |
Beta Was this translation helpful? Give feedback.
-
Perhaps the "name" or "inheritAttrs" could be used as attributes like this <script
setup
name="ComponentName"
inheritAttrs="false"
>
.
.
.
</script> |
Beta Was this translation helpful? Give feedback.
-
Relevant: vuejs/core#4997 is merged and component using |
Beta Was this translation helpful? Give feedback.
-
SummaryBased on the above discussion and the implemented plugins, there seem to be two ways to implement this feature.
|
Beta Was this translation helpful? Give feedback.
-
If it's only used for
|
Beta Was this translation helpful? Give feedback.
-
I don't recommend.
|
Beta Was this translation helpful? Give feedback.
-
error:
|
Beta Was this translation helpful? Give feedback.
-
i have question
i write in vue.config.js and defineOptions use in Header Component
|
Beta Was this translation helpful? Give feedback.
-
@yyx990803 So this PR #432 can be merged now? |
Beta Was this translation helpful? Give feedback.
-
Is there a reason or a technical impossibility to not returning an object to define options? <script setup lang="tsx">
//...
return {
render() {
return <h1>Hello World</h1>
}
};
</script> |
Beta Was this translation helpful? Give feedback.
-
Summary
Introduce a macro in script setup,
defineOptions
, to use Options API inscript setup
, specifically to be able to setname
,inheritAttrs
andrender
inscript setup
.Basic example
Compiled Output
JSX in
script-setup
Compiled Output
With Babel plugin.
Motivation
This proposal is mainly to set the Options API using only
script setup
.We already have
<script setup>
, but some options still need to be set in normal script tags (such asname
andinheritAttrs
). The goal of this proposal is to allow users to avoid script tag at all.Detailed design
Overview
The behavior of
defineOptions
is basically the same asdefineProps
.defineOptions
is only enabled in<script setup>
and without normal script tag.defineOptions
is compiler macros only usable inside<script setup>
. They do not need to be imported, and are compiled away when<script setup>
is processed.defineOptions
will be hoisted out of setup into module scope. Therefore, the options cannot reference local variables declared in setup scope. Doing so will result in a compile error. However, it can reference imported bindings since they are in the module scope as well.Drawbacks
Alternatives
Adoption strategy
This feature is opt-in. Existing SFC usage is unaffected.
Unresolved questions
Appendix
Enabling the Macros
I made a very prototype plugin unplugin-vue-define-options.
It supports Vite, Rollup, webpack, Vue CLI and ESBuild powered by unplugin.
Beta Was this translation helpful? Give feedback.
All reactions