vue-lynx / defineModel

Function: defineModel()

defineModel(options)

function defineModel<T, M, G, S>(options): ModelRef<T, M, G, S>

Vue <script setup> compiler macro for declaring a two-way binding prop that can be consumed via v-model from the parent component. This will declare a prop with the same name and a corresponding update:propName event.

If the first argument is a string, it will be used as the prop name; Otherwise the prop name will default to "modelValue". In both cases, you can also pass an additional object which will be used as the prop's options.

The returned ref behaves differently depending on whether the parent provided the corresponding v-model props or not:

  • If yes, the returned ref's value will always be in sync with the parent prop.
  • If not, the returned ref will behave like a normal local ref.

Type Parameters

Type ParameterDefault type
T-
M extends PropertyKeystring
GT
ST

Parameters

ParameterType
optionsobject | object & PropOptions<T, T> & DefineModelOptions<T, G, S>

Returns

ModelRef<T, M, G, S>

Example

// default model (consumed via `v-model`)
const modelValue = defineModel<string>()
modelValue.value = "hello"

// default model with options
const modelValue = defineModel<string>({ required: true })

// with specified name (consumed via `v-model:count`)
const count = defineModel<number>('count')
count.value++

// with specified name and default value
const count = defineModel<number>('count', { default: 0 })

Defined in

node_modules/.pnpm/@vue+runtime-core@3.5.30/node_modules/@vue/runtime-core/dist/runtime-core.d.ts:316

defineModel(options)

function defineModel<T, M, G, S>(options?): ModelRef<T | undefined, M, G | undefined, S | undefined>

Type Parameters

Type ParameterDefault type
T-
M extends PropertyKeystring
GT
ST

Parameters

ParameterType
options?PropOptions<T, T> & DefineModelOptions<T, G, S>

Returns

ModelRef<T | undefined, M, G | undefined, S | undefined>

Defined in

node_modules/.pnpm/@vue+runtime-core@3.5.30/node_modules/@vue/runtime-core/dist/runtime-core.d.ts:321

defineModel(name, options)

function defineModel<T, M, G, S>(name, options): ModelRef<T, M, G, S>

Type Parameters

Type ParameterDefault type
T-
M extends PropertyKeystring
GT
ST

Parameters

ParameterType
namestring
optionsobject | object & PropOptions<T, T> & DefineModelOptions<T, G, S>

Returns

ModelRef<T, M, G, S>

Defined in

node_modules/.pnpm/@vue+runtime-core@3.5.30/node_modules/@vue/runtime-core/dist/runtime-core.d.ts:322

defineModel(name, options)

function defineModel<T, M, G, S>(name, options?): ModelRef<T | undefined, M, G | undefined, S | undefined>

Type Parameters

Type ParameterDefault type
T-
M extends PropertyKeystring
GT
ST

Parameters

ParameterType
namestring
options?PropOptions<T, T> & DefineModelOptions<T, G, S>

Returns

ModelRef<T | undefined, M, G | undefined, S | undefined>

Defined in

node_modules/.pnpm/@vue+runtime-core@3.5.30/node_modules/@vue/runtime-core/dist/runtime-core.d.ts:327