Skip to content

Commit 2ec75d6

Browse files
authored
Merge pull request #72 from anotherglitchinthematrix/patch-1
fix: `jsonEditor.value` could be `undefined` under some certain situation in Vue 2
2 parents eca9325 + 10e62a9 commit 2ec75d6

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/Component.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default defineComponent({
4040
]),
4141
),
4242
} as {
43-
[key in ModelValueProp]: {}
43+
[key in ModelValueProp]: object
4444
} & { mode: { type: PropType<Mode> } } & {
4545
[key in typeof boolAttrs[number]]: {
4646
type: PropType<boolean>
@@ -90,7 +90,7 @@ export default defineComponent({
9090
emit('update:mode', mode)
9191
}
9292

93-
const mergeFunction = (previousValue: Function, currentValue: Function) => (...args: any) => {
93+
const mergeFunction = (previousValue: (...args: any) => unknown, currentValue: (...args: any) => unknown) => (...args: any) => {
9494
previousValue(...args)
9595
currentValue(...args)
9696
}
@@ -144,7 +144,7 @@ export default defineComponent({
144144
watch(
145145
() => props.mode,
146146
(mode) => {
147-
// `jsonEditor.value` could be `undefined` in Vue 2.6 (dev environment)
147+
// `jsonEditor.value` could be `undefined` in Vue 2.6
148148
jsonEditor.value?.updateProps({
149149
mode,
150150
})
@@ -154,7 +154,7 @@ export default defineComponent({
154154
watch(
155155
() => Array.from(boolAttrs, boolAttr => props[boolAttr]),
156156
(values) => {
157-
jsonEditor.value.updateProps(
157+
jsonEditor.value?.updateProps(
158158
Object.fromEntries(Array.from(values, (v, i) => [boolAttrs[i], v]).filter(([, v]) => v !== undefined)),
159159
)
160160
},
@@ -165,16 +165,16 @@ export default defineComponent({
165165
(newAttrs) => {
166166
// Functions need to be merged again
167167
const defaultFunctionAttrs: {
168-
onChange?: Function
169-
onChangeMode?: Function
168+
onChange?: (...args: any) => unknown
169+
onChangeMode?: (...args: any) => unknown
170170
} = {}
171171
if (newAttrs.onChange) {
172172
defaultFunctionAttrs.onChange = onChange
173173
}
174174
if (newAttrs.onChangeMode) {
175175
defaultFunctionAttrs.onChangeMode = onChangeMode
176176
}
177-
jsonEditor.value.updateProps(
177+
jsonEditor.value?.updateProps(
178178
Object.getOwnPropertyNames(defaultFunctionAttrs).length > 0
179179
? conclude([newAttrs, defaultFunctionAttrs], {
180180
type: Object,
@@ -191,7 +191,7 @@ export default defineComponent({
191191
expose?.({ jsonEditor })
192192

193193
onUnmounted(() => {
194-
jsonEditor.value.destroy()
194+
jsonEditor.value?.destroy()
195195
})
196196

197197
onMounted(() => {

0 commit comments

Comments
 (0)