Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| snippets:vue [2025/08/08 07:45] – [tsx table] lingao | snippets:vue [2025/11/05 12:18] (current) – [tsx slots type] lingao | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ==== tsx component best practice ==== | ==== tsx component best practice ==== | ||
| bare bone | bare bone | ||
| + | |||
| + | <code tsx> | ||
| + | import { computed, defineComponent, | ||
| + | |||
| + | type ModelType = __MODEL_TYPE__; | ||
| + | |||
| + | export default defineComponent({ | ||
| + | props: { | ||
| + | modelValue: { | ||
| + | type: Object as PropType< | ||
| + | default: () => ({} as ModelType), | ||
| + | }, | ||
| + | ' | ||
| + | type: Function as PropType< | ||
| + | }, | ||
| + | }, | ||
| + | setup(props) { | ||
| + | const modelValue = computed({ | ||
| + | get() { | ||
| + | return props.modelValue; | ||
| + | }, | ||
| + | set(val: ModelType) { | ||
| + | props[' | ||
| + | }, | ||
| + | }); | ||
| + | |||
| + | return () => <></>; | ||
| + | }, | ||
| + | }); | ||
| + | |||
| + | </ | ||
| + | |||
| <code tsx> | <code tsx> | ||
| import { defineComponent } from ' | import { defineComponent } from ' | ||
| Line 17: | Line 49: | ||
| <code tsx> | <code tsx> | ||
| import { defineComponent } from ' | import { defineComponent } from ' | ||
| - | export interface Expose {} | ||
| export default defineComponent({ | export default defineComponent({ | ||
| Line 150: | Line 181: | ||
| </ | </ | ||
| </ | </ | ||
| - | <ElTable class=" | + | <ElTable class=" |
| < | < | ||
| < | < | ||
| Line 249: | Line 280: | ||
| }) | }) | ||
| </ | </ | ||
| + | ++++ | ||
| + | |||
| + | ==== tsx add ==== | ||
| + | |||
| + | ++++ tsx add | | ||
| + | <code tsx> | ||
| + | import { defineComponent, | ||
| + | import BaseContainer from ' | ||
| + | import Form from ' | ||
| + | import { AssessmentObjective } from ' | ||
| + | import { ElButton } from ' | ||
| + | |||
| + | export default defineComponent({ | ||
| + | setup() { | ||
| + | const form = ref< | ||
| + | |||
| + | function handleSubmit() {} | ||
| + | |||
| + | return () => ( | ||
| + | < | ||
| + | {{ | ||
| + | default: () => <Form v-model={form.value} />, | ||
| + | ' | ||
| + | < | ||
| + | 提交 | ||
| + | </ | ||
| + | ) | ||
| + | }} | ||
| + | </ | ||
| + | ) | ||
| + | } | ||
| + | }) | ||
| + | |||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ==== tsx slots type ==== | ||
| + | |||
| + | ++++ tsx slots type | | ||
| + | < | ||
| + | const Component = defineComponent({ | ||
| + | props: { | ||
| + | config: { | ||
| + | type: Array as PropType< | ||
| + | } | ||
| + | }, | ||
| + | slots: Object as SlotsType< | ||
| + | default: ({ foo: string; bar: number }) => JSX.Element | ||
| + | }>, | ||
| + | setup: (props, { slots }) => { | ||
| + | if (props.config == null) throw Error(' | ||
| + | const context = createReferenceInstance(props.config) | ||
| + | provide(refInjectionKey, | ||
| + | return () => <> | ||
| + | } | ||
| + | }) | ||
| + | |||
| + | const s = new Component().$slots | ||
| + | |||
| + | const a = ( | ||
| + | < | ||
| + | { | ||
| + | { | ||
| + | default: (scope) => { | ||
| + | scope | ||
| + | } | ||
| + | } as typeof s | ||
| + | } | ||
| + | </ | ||
| + | ) | ||
| + | </ | ||
| ++++ | ++++ | ||