Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mengliang.wang
committed
Feb 5, 2021
1 parent
04656f4
commit 7b6db22
Showing
4 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { computed, defineComponent, ref, watch } from "vue"; | ||
|
||
// 用jsx封装组件的时候,实现双向数据绑定 | ||
export function useModel<T>(getter: () => T, emitter: (val: T) => void) { | ||
const state = ref(getter()) as { value: T }; | ||
|
||
watch(getter, (val) => { | ||
if (val !== state.value) { | ||
state.value = val; | ||
} | ||
}); | ||
|
||
return { | ||
get value() { | ||
return state.value; | ||
}, | ||
set value(val: T) { | ||
if (state.value !== val) { | ||
state.value = val; | ||
emitter(val); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
// modelValue 外部可以用v-model绑定 | ||
export const TestUseModel = defineComponent({ | ||
props: { | ||
modelValue: { type: String }, | ||
}, | ||
emits: { | ||
"update:modelValue": (val?: string) => true, | ||
}, | ||
setup(props, ctx) { | ||
const model = useModel( | ||
() => props.modelValue, | ||
(val) => ctx.emit("update:modelValue", val) | ||
); | ||
return () => ( | ||
<div> | ||
自定义输入框 | ||
<input type="text" v-model={model.value} /> | ||
</div> | ||
); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface VisualEditorBlockData { | ||
top: number; | ||
left: number; | ||
} | ||
|
||
export interface VisualEditorModelValue { | ||
container: { | ||
width: number; | ||
height: number; | ||
}; | ||
blocks: VisualEditorBlockData[]; | ||
} |