Skip to content

Commit bf3fabb

Browse files
committed
refactor:模板引用升级为 vue 3.5 新增的 useTemplateRef
1 parent 7414be1 commit bf3fabb

48 files changed

Lines changed: 186 additions & 201 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/admin/library/crud/stubs/html/form.stub

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@
4444
</template>
4545

4646
<script setup lang="ts">
47-
import type { FormInstance, FormItemRule } from 'element-plus'
48-
import { inject, reactive, ref } from 'vue'
47+
import type { FormItemRule } from 'element-plus'
48+
import { inject, reactive, useTemplateRef } from 'vue'
4949
import { useI18n } from 'vue-i18n'
5050
import FormItem from '/@/components/formItem/index.vue'
5151
import { useConfig } from '/@/stores/config'
5252
import type baTableClass from '/@/utils/baTable'
5353
import { buildValidatorData } from '/@/utils/validate'
5454

5555
const config = useConfig()
56-
const formRef = ref<FormInstance>()
56+
const formRef = useTemplateRef('formRef')
5757
const baTable = inject('baTable') as baTableClass
5858

5959
const { t } = useI18n()

app/admin/library/crud/stubs/html/index.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</template>
2121

2222
<script setup lang="ts">
23-
import { onMounted, provide, ref } from 'vue'
23+
import { onMounted, provide, useTemplateRef } from 'vue'
2424
import { useI18n } from 'vue-i18n'
2525
import PopupForm from './popupForm.vue'
2626
import { baTableApi } from '/@/api/common'
@@ -34,7 +34,7 @@ defineOptions({
3434
})
3535

3636
const { t } = useI18n()
37-
const tableRef = ref()
37+
const tableRef = useTemplateRef('tableRef')
3838
const optButtons: OptButton[] = defaultOptButtons({%optButtons%})
3939

4040
/**

web/src/components/baInput/components/baUpload.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
</template>
5151

5252
<script setup lang="ts">
53-
import { ref, reactive, onMounted, watch, useAttrs, nextTick } from 'vue'
53+
import { reactive, onMounted, watch, useAttrs, nextTick, useTemplateRef } from 'vue'
5454
import { genFileId } from 'element-plus'
55-
import type { UploadInstance, UploadUserFile, UploadProps, UploadRawFile, UploadFiles } from 'element-plus'
55+
import type { UploadUserFile, UploadProps, UploadRawFile, UploadFiles } from 'element-plus'
5656
import { stringToArray } from '/@/components/baInput/helper'
5757
import { fullUrl, arrayFullUrl, getFileNameFromPath, getArrayKey } from '/@/utils/common'
5858
import { fileUpload } from '/@/api/common'
@@ -110,7 +110,7 @@ const emits = defineEmits<{
110110
}>()
111111
112112
const attrs = useAttrs()
113-
const upload = ref<UploadInstance>()
113+
const upload = useTemplateRef('upload')
114114
const state: {
115115
key: string
116116
// 返回值类型,通过v-model类型动态计算

web/src/components/baInput/components/iconSelector.vue

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@
2727
</span>
2828
</div>
2929
</div>
30-
<div class="selector-body">
31-
<el-scrollbar ref="selectorScrollbarRef">
32-
<div v-if="renderFontIconNames.length > 0">
33-
<div class="icon-selector-item" :title="item" @click="onIcon(item)" v-for="(item, key) in renderFontIconNames" :key="key">
34-
<Icon :name="item" />
35-
</div>
30+
<el-scrollbar class="selector-body">
31+
<div v-if="renderFontIconNames.length > 0">
32+
<div class="icon-selector-item" :title="item" @click="onIcon(item)" v-for="(item, key) in renderFontIconNames" :key="key">
33+
<Icon :name="item" />
3634
</div>
37-
</el-scrollbar>
38-
</div>
35+
</div>
36+
</el-scrollbar>
3937
</div>
4038
</div>
4139
<template #reference>
@@ -66,7 +64,7 @@
6664
<script setup lang="ts">
6765
import { useEventListener } from '@vueuse/core'
6866
import type { Placement } from 'element-plus'
69-
import { computed, nextTick, onMounted, reactive, ref, watch } from 'vue'
67+
import { computed, nextTick, onMounted, reactive, useTemplateRef, watch } from 'vue'
7068
import { getAwesomeIconfontNames, getElementPlusIconfontNames, getIconfontNames, getLocalIconfontNames } from '/@/utils/iconfont'
7169
7270
type IconType = 'ele' | 'awe' | 'ali' | 'local'
@@ -95,8 +93,7 @@ const emits = defineEmits<{
9593
(e: 'change', value: string): void
9694
}>()
9795
98-
const selectorInput = ref()
99-
const selectorScrollbarRef = ref()
96+
const selectorInput = useTemplateRef('selectorInput')
10097
const state: {
10198
iconType: IconType
10299
selectorWidth: number
@@ -164,7 +161,7 @@ const onIcon = (icon: string) => {
164161
emits('update:modelValue', icon)
165162
emits('change', icon)
166163
nextTick(() => {
167-
selectorInput.value.blur()
164+
selectorInput.value?.blur()
168165
})
169166
}
170167
@@ -182,7 +179,7 @@ const renderFontIconNames = computed(() => {
182179
// 获取 input 的宽度
183180
const getInputWidth = () => {
184181
nextTick(() => {
185-
state.selectorWidth = selectorInput.value.$el.offsetWidth < 260 ? 260 : selectorInput.value.$el.offsetWidth
182+
state.selectorWidth = selectorInput.value?.$el.offsetWidth < 260 ? 260 : selectorInput.value?.$el.offsetWidth
186183
})
187184
}
188185

web/src/components/baInput/components/remoteSelect.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
<script lang="ts" setup>
6767
import type { ElSelect } from 'element-plus'
6868
import { debounce, isEmpty } from 'lodash-es'
69-
import { computed, getCurrentInstance, nextTick, onMounted, onUnmounted, reactive, ref, toRaw, useAttrs, watch } from 'vue'
69+
import { computed, getCurrentInstance, nextTick, onMounted, onUnmounted, reactive, toRaw, useAttrs, useTemplateRef, watch } from 'vue'
7070
import { InputAttr } from '../index'
7171
import { getSelectData } from '/@/api/common'
7272
import { useConfig } from '/@/stores/config'
@@ -75,7 +75,7 @@ import { shortUuid } from '/@/utils/random'
7575
7676
const attrs = useAttrs()
7777
const config = useConfig()
78-
const selectRef = ref<InstanceType<typeof ElSelect> | undefined>()
78+
const selectRef = useTemplateRef('selectRef')
7979
type ElSelectProps = Omit<Partial<InstanceType<typeof ElSelect>['$props']>, 'modelValue'>
8080
type valueTypes = string | number | string[] | number[]
8181

web/src/components/baInput/components/selectFile.vue

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</template>
4040

4141
<script setup lang="ts">
42-
import { ref, reactive, onMounted, provide, watch, nextTick } from 'vue'
42+
import { reactive, onMounted, provide, watch, nextTick, useTemplateRef } from 'vue'
4343
import { useI18n } from 'vue-i18n'
4444
import Table from '/@/components/table/index.vue'
4545
import TableHeader from '/@/components/table/header/index.vue'
@@ -66,13 +66,14 @@ const emits = defineEmits<{
6666
(e: 'choice', value: string[]): void
6767
}>()
6868
69-
const tableRef = ref()
7069
const { t } = useI18n()
7170
const state = reactive({
7271
ready: false,
7372
tableSelectable: true,
7473
})
7574
75+
const tableRef = useTemplateRef('tableRef')
76+
7677
const optBtn: OptButton[] = [
7778
{
7879
render: 'tipButton',
@@ -83,8 +84,8 @@ const optBtn: OptButton[] = [
8384
class: 'table-row-choice',
8485
disabledTip: false,
8586
click: (row: TableRow) => {
86-
const elTableRef = tableRef.value.getRef()
87-
elTableRef.clearSelection()
87+
const elTableRef = tableRef.value?.getRef()
88+
elTableRef?.clearSelection()
8889
emits('choice', props.returnFullUrl ? [row.full_url] : [row.url])
8990
},
9091
},
@@ -198,16 +199,16 @@ const onChoice = () => {
198199
files.push(props.returnFullUrl ? baTable.table.selection[key].full_url : baTable.table.selection[key].url)
199200
}
200201
emits('choice', files)
201-
const elTableRef = tableRef.value.getRef()
202-
elTableRef.clearSelection()
202+
const elTableRef = tableRef.value?.getRef()
203+
elTableRef?.clearSelection()
203204
}
204205
}
205206
206207
const onSelectionChange = (selection: TableRow[]) => {
207208
if (props.limit == 0) return
208209
if (selection.length > props.limit) {
209-
const elTableRef = tableRef.value.getRef()
210-
elTableRef.toggleRowSelection(selection[selection.length - 1], false)
210+
const elTableRef = tableRef.value?.getRef()
211+
elTableRef?.toggleRowSelection(selection[selection.length - 1], false)
211212
}
212213
state.tableSelectable = !(selection.length >= props.limit)
213214
}

web/src/components/table/index.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@
6363
</template>
6464

6565
<script setup lang="ts">
66-
import type { ElTable, TableInstance } from 'element-plus'
66+
import type { ElTable } from 'element-plus'
6767
import type { Component } from 'vue'
68-
import { computed, inject, nextTick, ref } from 'vue'
68+
import { computed, inject, nextTick, useTemplateRef } from 'vue'
6969
import { useConfig } from '/@/stores/config'
7070
import type baTableClass from '/@/utils/baTable'
7171
import { shortUuid } from '/@/utils/random'
7272
7373
const config = useConfig()
74-
const tableRef = ref<TableInstance>()
74+
const tableRef = useTemplateRef('tableRef')
7575
const baTable = inject('baTable') as baTableClass
7676
type ElTableProps = Partial<InstanceType<typeof ElTable>['$props']>
7777

web/src/components/terminal/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
<script setup lang="ts">
183183
import type { TimelineItemProps } from 'element-plus'
184184
import { ElMessageBox, ElScrollbar } from 'element-plus'
185-
import { nextTick, onMounted, reactive, ref } from 'vue'
185+
import { nextTick, onMounted, reactive, useTemplateRef } from 'vue'
186186
import { useI18n } from 'vue-i18n'
187187
import { postChangeTerminalConfig } from '/@/api/common'
188188
import FormItem from '/@/components/formItem/index.vue'
@@ -194,7 +194,7 @@ type SourceType = 'npm' | 'composer'
194194
195195
const { t } = useI18n()
196196
const terminal = useTerminal()
197-
const terminalScrollbarRef = ref<InstanceType<typeof ElScrollbar>>()
197+
const terminalScrollbarRef = useTemplateRef('terminalScrollbarRef')
198198
199199
const state = reactive({
200200
registryLoading: false,

web/src/layouts/backend/components/baAccount.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
</template>
7878

7979
<script setup lang="ts">
80-
import type { FormInstance, FormItemRule } from 'element-plus'
81-
import { reactive, ref, watch } from 'vue'
80+
import type { FormItemRule } from 'element-plus'
81+
import { reactive, useTemplateRef, watch } from 'vue'
8282
import { useI18n } from 'vue-i18n'
8383
import { baAccountCheckIn, baAccountGetUserInfo } from '/@/api/backend/index'
8484
import clickCaptcha from '/@/components/clickCaptcha'
@@ -92,7 +92,7 @@ const { t } = useI18n()
9292
const baAccount = useBaAccount()
9393
const siteConfig = useSiteConfig()
9494
const model = defineModel<boolean>()
95-
const baAccountFormRef = ref<FormInstance>()
95+
const baAccountFormRef = useTemplateRef('baAccountFormRef')
9696
9797
interface Props {
9898
loginCallback?: (res: ApiResponse) => void
@@ -116,8 +116,7 @@ const state = reactive({
116116
})
117117
118118
const onBaAccountSubmitPre = () => {
119-
if (!baAccountFormRef.value) return
120-
baAccountFormRef.value.validate((valid) => {
119+
baAccountFormRef.value?.validate((valid) => {
121120
if (valid) {
122121
clickCaptcha(state.user.captchaId, (captchaInfo: string) => onBaAccountSubmit(captchaInfo), { apiBaseURL: siteConfig.apiUrl })
123122
}

web/src/layouts/backend/components/config.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="layout-config-drawer">
33
<el-drawer :model-value="configStore.layout.showDrawer" :title="t('layouts.Layout configuration')" size="310px" @close="onCloseDrawer">
44
<el-scrollbar class="layout-mode-style-scrollbar">
5-
<el-form ref="formRef" :model="configStore.layout">
5+
<el-form :model="configStore.layout">
66
<div class="layout-mode-styles-box">
77
<el-divider border-style="dashed">{{ t('layouts.Layout mode') }}</el-divider>
88
<div class="layout-mode-box-style">

0 commit comments

Comments
 (0)