Appearance
Value Model Mixin
File Information
- Path:
mixins/ValueModel.js - Purpose: Simple v-model wrapper for boolean values
Overview
The Value Model mixin provides a simple two-way binding wrapper for boolean values. It creates a local model property that syncs with the value prop and emits input events, enabling v-model functionality for components.
Key Features
Two-Way Binding
- Syncs prop to local model
- Emits input events
- Reactive updates
Simple Implementation
- Minimal code
- Automatic syncing
- Vue reactivity
Props
javascript
{
value: {
type: Boolean,
default: false,
description: 'Boolean value prop for v-model'
}
}Data Properties
javascript
{
model: boolean // Local model value (initialized from prop)
}Watchers
value
Watches value prop changes:
- Updates local
modelwhen prop changes - Keeps model in sync with prop
model
Watches model changes:
- Emits
inputevent when model changes - Enables v-model functionality
Usage Examples
Basic Usage
vue
<template>
<div>
<input
type="checkbox"
v-model="model"
/>
<p>Value: {{ model }}</p>
</div>
</template>
<script>
import valueModel from '~/mixins/value-model'
export default {
mixins: [valueModel]
// Component can now be used with v-model
}
</script>Parent Component
vue
<template>
<MyComponent v-model="isEnabled" />
</template>
<script>
export default {
data() {
return {
isEnabled: false
}
}
}
</script>How It Works
Initialization
modelis initialized fromvalueprop- Component has local state
Prop Update
- When
valueprop changes,modelupdates - Watcher syncs prop to model
- When
Model Update
- When
modelchanges,inputevent is emitted - Parent component receives update
- v-model updates parent value
- When
Integration Points
- Form Components: Checkbox, toggle components
- Dialog Components: Visibility toggles
- Any Component: That needs v-model for boolean
Notes for Development
- Mixin is very simple and lightweight
- Only works with boolean values
- Provides standard v-model pattern
- Automatic two-way binding
- Minimal overhead
Related Documentation
- Vue.js v-model - Vue.js documentation
- Component Documentation - Component usage