From aa3629dd16520c5839a0d532dae8d8c6990d5035 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 16 Feb 2022 00:07:24 +0800 Subject: [PATCH 1/4] add defineOptions proposal --- active-rfcs/0000-define-options.md | 157 +++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 active-rfcs/0000-define-options.md diff --git a/active-rfcs/0000-define-options.md b/active-rfcs/0000-define-options.md new file mode 100644 index 00000000..46d5b056 --- /dev/null +++ b/active-rfcs/0000-define-options.md @@ -0,0 +1,157 @@ +- Start Date: 2022-02-13 +- Target Major Version: 3.x +- Reference Issues: https://github.com/vuejs/core/issues/5218 +- Implementation PR: + +# Summary + +Introduce a macro in script setup, `defineOptions`, to use Options API in script setup, specifically to be able to set `name`, `props`, `emits` and `render` in one function. + +# Basic example + +```vue + +``` + +
+Compiled Output + +```js +const __default__ = { + name: 'Foo', + inheritAttrs: false, + props: { + msg: { type: String, required: false } + }, + emits: ['change', 'update'] +} +const setup = () => { + const slots = useSlots() + return { slots } +} +export default Object.assign(__default__, { + setup +}) +``` + +
+ +### JSX in `script-setup` + +```vue + +``` + +
+Compiled Output + +```js +const __default__ = { + render() { + return h('h1', {}, () => 'Hello World') + } +} +const setup = () => {} +export default Object.assign(__default__, { + setup +}) +``` + +
+ +### With [Reactivity Transform](https://github.com/vuejs/rfcs/pull/420) + +With (reactivity transform)[https://github.com/vuejs/rfcs/pull/420], it is also possible to set a default value. + +```vue + +``` + +
+Compiled Output + +```js +const __default__ = { + props: { + msg: { type: String, required: false, default: 'hello' } + } +} +const setup = (__props) => { + console.log(__props.msg) +} +export default Object.assign(__default__, { + setup +}) +``` + +
+ +# Motivation + +This proposal is mainly to unify Options API, `defineProps` and `defineEmits`. + +We already have ` ``` @@ -34,11 +29,7 @@ const slots = useSlots() ```js const __default__ = { name: 'Foo', - inheritAttrs: false, - props: { - msg: { type: String, required: false } - }, - emits: ['change', 'update'] + inheritAttrs: false } const setup = () => { const slots = useSlots() @@ -66,6 +57,8 @@ defineOptions({
Compiled Output +With [Babel plugin](https://github.com/vuejs/babel-plugin-jsx). + ```js const __default__ = { render() { @@ -80,50 +73,12 @@ export default Object.assign(__default__, {
-### With [Reactivity Transform](https://github.com/vuejs/rfcs/pull/420) - -With (reactivity transform)[https://github.com/vuejs/rfcs/pull/420], it is also possible to set a default value. - -```vue - -``` - -
-Compiled Output - -```js -const __default__ = { - props: { - msg: { type: String, required: false, default: 'hello' } - } -} -const setup = (__props) => { - console.log(__props.msg) -} -export default Object.assign(__default__, { - setup -}) -``` - -
- # Motivation -This proposal is mainly to unify Options API, `defineProps` and `defineEmits`. +This proposal is mainly to set the Options API using only `script setup`. We already have `