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 01:11] – [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 74: | Line 105: | ||
| import { ElForm, ElFormItem, ElInput, ElTable, ElTableColumn, | import { ElForm, ElFormItem, ElInput, ElTable, ElTableColumn, | ||
| import { defineComponent, | import { defineComponent, | ||
| - | import { useRoute, useRouter | + | import {ItemType} from './types' |
| type PaginationConfiguration = Pick< | type PaginationConfiguration = Pick< | ||
| - | type QueryParams = Pick< | + | type QueryParams = Partial<Pick< |
| - | type Item = {} | + | type Item = ItemType |
| export default defineComponent({ | export default defineComponent({ | ||
| setup() { | setup() { | ||
| - | const router = useRouter() | ||
| - | const route = useRoute() | ||
| const loading = ref(false) | const loading = ref(false) | ||
| const tableData = ref< | const tableData = ref< | ||
| Line 93: | Line 122: | ||
| }) | }) | ||
| - | | + | |
| getTableData() | getTableData() | ||
| } | } | ||
| - | | + | |
| queryParams.value = {} | queryParams.value = {} | ||
| getTableData() | getTableData() | ||
| } | } | ||
| function handleAdd() {} | function handleAdd() {} | ||
| + | // @ts-expect-error | ||
| function handleEdit(row: | function handleEdit(row: | ||
| + | // @ts-expect-error | ||
| function handleDelete(row: | function handleDelete(row: | ||
| + | |||
| async function getTableData() { | async function getTableData() { | ||
| - | const res = {} as any | + | |
| + | | ||
| loading.value = true | loading.value = true | ||
| // paginationConfig.value.total = | // paginationConfig.value.total = | ||
| Line 148: | Line 181: | ||
| </ | </ | ||
| </ | </ | ||
| - | <ElTable class=" | + | <ElTable class=" |
| < | < | ||
| < | < | ||
| Line 167: | Line 200: | ||
| < | < | ||
| {(scope) => ( | {(scope) => ( | ||
| - | <div class=" | + | <div class=" |
| < | < | ||
| link | link | ||
| type=" | type=" | ||
| - | icon=" | ||
| onClick={() => handleEdit(scope.row)} | onClick={() => handleEdit(scope.row)} | ||
| > | > | ||
| Line 179: | Line 211: | ||
| link | link | ||
| type=" | type=" | ||
| - | icon=" | ||
| onClick={() => handleDelete(scope.row)} | onClick={() => handleDelete(scope.row)} | ||
| > | > | ||
| Line 204: | Line 235: | ||
| }) | }) | ||
| </ | </ | ||
| + | ++++ | ||
| + | ==== tsx _form ==== | ||
| + | |||
| + | ++++ tsx _form | | ||
| + | <code tsx> | ||
| + | import { computed, defineComponent, | ||
| + | import BaseCard from ' | ||
| + | import type { AssessmentObjective } from ' | ||
| + | import { ElForm, ElFormItem } from ' | ||
| + | |||
| + | const rules = {} | ||
| + | |||
| + | export default defineComponent({ | ||
| + | props: { | ||
| + | modelValue: { | ||
| + | type: Object as PropType< | ||
| + | default: () => ({}) | ||
| + | } | ||
| + | }, | ||
| + | emits: [' | ||
| + | setup(props, | ||
| + | const modelValue = computed({ | ||
| + | get() { | ||
| + | return props.modelValue | ||
| + | }, | ||
| + | set(val) { | ||
| + | emit(' | ||
| + | } | ||
| + | }) | ||
| + | |||
| + | return () => ( | ||
| + | <ElForm model={modelValue.value} rules={rules} class=" | ||
| + | < | ||
| + | <div class=" | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | ) | ||
| + | } | ||
| + | }) | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ==== 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 | ||
| + | } | ||
| + | </ | ||
| + | ) | ||
| + | </ | ||
| ++++ | ++++ | ||