Skip to content

defineOptions NPM Version

Stability: stable

Options API can be declared using the defineOptions in <script setup>, specifically to be able to set name, props, emits, and render inside of one function.

For Vue >= 3.3, this feature will be turned off by default.

FeaturesSupported
Vue 3
Nuxt 3
Vue 2
TypeScript

Installation Standalone Version

if you need defineOptions feature only, the standalone version is more appropriate for you.

Installation

bash
npm i -D unplugin-vue-define-options @vue-macros/volar
bash
yarn add -D unplugin-vue-define-options @vue-macros/volar
bash
pnpm add -D unplugin-vue-define-options @vue-macros/volar
ts
// vite.config.ts
import DefineOptions from 'unplugin-vue-define-options/vite'

export default defineConfig({
  plugins: [DefineOptions()],
})
ts
// rollup.config.js
import DefineOptions from 'unplugin-vue-define-options/rollup'

export default {
  plugins: [DefineOptions()],
}
js
// esbuild.config.js
import { build } from 'esbuild'

build({
  plugins: [require('unplugin-vue-define-options/esbuild')()],
})
js
// webpack.config.js
module.exports = {
  /* ... */
  plugins: [require('unplugin-vue-define-options/webpack')()],
}

TypeScript Support

tsconfig.json
json
{
  "compilerOptions": {
    // ...
    "types": ["unplugin-vue-define-options/macros-global" /* ... */]
  }
}

Basic Usage

vue
<script setup lang="ts">
defineOptions
({
name
: 'Foo',
inheritAttrs
: false,
})
defineProps
<{
foo
: number
}>() </script> <template> <
Foo
:foo
="1" />
</template>
Compiled Code
vue
<script lang="ts">
export default {
  
name
: 'Foo',
inheritAttrs
: false,
} </script> <script setup lang="ts">
defineProps
<{
foo
: number
}>() </script> <template> <
Foo
:foo
="1" />
</template>

JSX in <script setup>

vue
<script setup lang="tsx">
defineOptions
({
render
() {
return <
h1
>Hello World</
h1
>
}, }) </script>
Compiled Code
vue
<script lang="tsx">
export default {
  render() {
    return <h1>Hello World</h1>
  },
}
</script>

Volar Configuration

tsconfig.json
jsonc
{
  "vueCompilerOptions": {
    "plugins": ["vue-macros/volar"],
  },
}

Contributors

Changelog

Layout Switch

Adjust the layout style of VitePress to adapt to different reading needs and screens.

Expand all
The sidebar and content area occupy the entire width of the screen.
Expand sidebar with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Expand all with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Original width
The original layout width of VitePress

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.

Spotlight

Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties.

ONOn
Turn on Spotlight.
OFFOff
Turn off Spotlight.