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/04 06:38] – 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 13: | Line 45: | ||
| ) | ) | ||
| + | </ | ||
| + | |||
| + | <code tsx> | ||
| + | import { defineComponent } from ' | ||
| + | |||
| + | export default defineComponent({ | ||
| + | setup() { | ||
| + | return () => <></> | ||
| + | } | ||
| + | }) | ||
| </ | </ | ||
| ++++ tsx component | | ++++ tsx component | | ||
| Line 63: | Line 105: | ||
| import { ElForm, ElFormItem, ElInput, ElTable, ElTableColumn, | import { ElForm, ElFormItem, ElInput, ElTable, ElTableColumn, | ||
| import { defineComponent, | import { defineComponent, | ||
| - | import { useRoute | + | import {ItemType} from './types' |
| - | / | + | type PaginationConfiguration = Pick< |
| - | * 类型定义 | + | type QueryParams |
| - | | + | type Item = ItemType |
| - | interface | + | |
| - | | + | |
| - | } | + | |
| - | + | ||
| - | interface | + | |
| - | personId: string | + | |
| - | personName: string | + | |
| - | personPhone: | + | |
| - | personAddress: | + | |
| - | } | + | |
| export default defineComponent({ | export default defineComponent({ | ||
| - | name: ' | ||
| - | |||
| setup() { | setup() { | ||
| - | / | ||
| - | * 公共变量定义 | ||
| - | | ||
| const loading = ref(false) | const loading = ref(false) | ||
| const tableData = ref< | const tableData = ref< | ||
| - | const queryParams = ref< | + | const queryParams = ref< |
| - | personId: '' | + | |
| - | | + | |
| const paginationConfig = ref< | const paginationConfig = ref< | ||
| total: 0, | total: 0, | ||
| Line 96: | Line 121: | ||
| pageSize: 10 | pageSize: 10 | ||
| }) | }) | ||
| - | const route = useRoute() | ||
| - | const projectId = route.query.parentId as string | ||
| - | | + | |
| - | * 业务逻辑 | + | |
| - | | + | |
| - | + | ||
| - | /** | + | |
| - | * 搜索表单初始化 | + | |
| - | */ | + | |
| - | + | ||
| - | /** | + | |
| - | * 表格初始化 | + | |
| - | */ | + | |
| - | + | ||
| - | const handleQuerySearch | + | |
| getTableData() | getTableData() | ||
| } | } | ||
| - | | + | |
| - | queryParams.value.personId | + | queryParams.value = {} |
| getTableData() | getTableData() | ||
| } | } | ||
| - | | + | |
| - | | + | |
| - | | + | function |
| - | | + | |
| - | const res = {} as any | + | function |
| + | |||
| + | | ||
| + | // @ts-expect-error | ||
| + | const res = { data: [{}] } | ||
| loading.value = true | loading.value = true | ||
| // paginationConfig.value.total = | // paginationConfig.value.total = | ||
| Line 128: | Line 143: | ||
| } | } | ||
| - | | + | |
| - | * 初始化 | + | if (e.key === ' |
| - | ********************/ | + | } |
| onMounted(() => { | onMounted(() => { | ||
| Line 147: | Line 162: | ||
| clearable | clearable | ||
| style=" | style=" | ||
| - | onKeydown={(e: KeyboardEvent) => { | + | onKeydown={handleKeydown} |
| - | if (e.key === ' | + | |
| - | }} | + | |
| /> | /> | ||
| </ | </ | ||
| Line 168: | Line 181: | ||
| </ | </ | ||
| </ | </ | ||
| - | <ElTable class=" | + | <ElTable class=" |
| < | < | ||
| < | < | ||
| Line 187: | Line 200: | ||
| < | < | ||
| {(scope) => ( | {(scope) => ( | ||
| - | <div class=" | + | <div class=" |
| < | < | ||
| link | link | ||
| type=" | type=" | ||
| - | icon=" | ||
| onClick={() => handleEdit(scope.row)} | onClick={() => handleEdit(scope.row)} | ||
| > | > | ||
| Line 199: | Line 211: | ||
| link | link | ||
| type=" | type=" | ||
| - | icon=" | ||
| onClick={() => handleDelete(scope.row)} | onClick={() => handleDelete(scope.row)} | ||
| > | > | ||
| Line 224: | 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 | ||
| + | } | ||
| + | </ | ||
| + | ) | ||
| + | </ | ||
| ++++ | ++++ | ||