Skip to content

Commit 6a44d6c

Browse files
committed
fix: v-model not working
1 parent 7eb6415 commit 6a44d6c

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

playground/app.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const extensions = [javascript({ jsx: true, typescript: true })]
1313
const handleChange = (value: string, viewUpdate: ViewUpdate) => {
1414
console.log('Value changed:', value)
1515
console.log('View updated:', viewUpdate)
16-
code.value = value
16+
// code.value = value
1717
}
1818
1919
const handleStatistics = (stats: Statistics) => {

src/runtime/components/NuxtCodeMirror.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ const container = ref<HTMLDivElement | null>(null)
1010
const view = ref<EditorView>()
1111
const state = ref<EditorState>()
1212
13-
const props = withDefaults(defineProps<NuxtCodeMirrorProps>(), {
14-
modelValue: '',
13+
const modelValue = defineModel<string>({ default: '' })
14+
15+
const props = withDefaults(defineProps<Omit<NuxtCodeMirrorProps, 'modelValue'>>(), {
1516
extensions: () => [],
1617
theme: 'light',
1718
})
@@ -35,7 +36,11 @@ onMounted(async () => {
3536
3637
useNuxtCodeMirror({
3738
...props,
38-
onChange: (value, viewUpdate) => emit('onChange', value, viewUpdate),
39+
modelValue: modelValue.value,
40+
onChange: (value, viewUpdate) => {
41+
modelValue.value = value
42+
emit('onChange', value, viewUpdate)
43+
},
3944
onStatistics: data => emit('onStatistics', data),
4045
onCreateEditor: (view, state) => emit('onCreateEditor', { view, state }),
4146
onUpdate: viewUpdate => emit('onUpdate', viewUpdate),
@@ -52,7 +57,7 @@ onMounted(async () => {
5257
// console.log('test: ', view.value)
5358
})
5459
55-
watch(() => props.modelValue, (newValue) => {
60+
watch(() => modelValue, (newValue) => {
5661
if (typeof newValue !== 'string') {
5762
console.error(`value must be typeof string but got ${typeof newValue}`)
5863
}

0 commit comments

Comments
 (0)