单文件组件sfc

编写模板

声明使用ts,setup标识使用composion API,组件导入在script import即可,其他template、style模块和vue2一样。
image.png

定义data、方法、生命周期函数、refs使用

image.png
推荐使用ref定义数据,使用数据的时候,一些地方需要.value获取;
注意:element ui 没有很好的TS类型支持,一些数据定义需要额外定义。(全局方法也是没有(需要全局定义type),好多需要补齐的地方,有点为了用而用了)
此外,refs的用法注意,vue composition 里面没有this了。

使用全局注册方法

image.pngimage.png

老项目体验(升级vue2.7+ts)

因为项目比较老旧的原因,vue2.7还是对TS的支持还是不太够:
例如取某个DOM定义的ref,获取元素调用某个方法时,需要定义:
image.png
导致方法调用的可选链过长(不然会告警):
image.png
老项目存留的js方法重构ts的成本也比较大。(如果不修改的话,会有很多地方使用any,感觉为了ts而ts了)

代码例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<!-- 2.出入省统计 -->
<script setup lang="ts">
import CenterMap from "@/views/index/CenterMap.vue";
import type {ComponentPublicInstance} from 'vue'
import {onMounted, ref, getCurrentInstance, onBeforeMount, onBeforeUnmount} from "vue";
import {deepClone} from '@/utils/index.js'
import {listFlow} from '@/api/biz/flow.js'
import consts from '@/utils/consts.js'
import {getPicture, getPicturePrefix} from '@/api/biz/picture.js'
import {allListPortalFrame} from '@/api/base/PortalFrame.js'

const {proxy} = getCurrentInstance() as any

const pictureUrlPrefix = ref<string>('')
const gateData = ref<Array<any>>([])
const defaultPageSize = 50

const entryCarList = ref<Array<any>>([])
const entryCarPageTotal = ref<number>(0)
const entryCarPageLoading = ref<boolean>(false)
const entryCarQueryParams = ref<{ pageNum: number, pageSize: number }>({
pageNum: 1,
pageSize: 50
})

const outCarList = ref<Array<any>>([])
const outCarPageTotal = ref<number>(0)
const outCarPageLoading = ref<boolean>(false)
const outCarQueryParams = ref<{ pageNum: number, pageSize: number }>({
pageNum: 1,
pageSize: 50
})

const refEntryCar = ref<HTMLElement & { bodyWrapper?: HTMLElement } | null>(null)
const refOutCar = ref<HTMLElement & { bodyWrapper?: HTMLElement } | null>(null)
const map = ref<ComponentPublicInstance & { resizeChart?: () => void, showInfo?: (any) => void } | null>(null);

onBeforeMount(() => {
getEntryCarList()
getOutCarList()
getPortalAllList()
getPictureUrlPrefix()
});

onMounted(() => {
window.addEventListener("resize", resizeChart)
})
onBeforeUnmount(() => {
window.removeEventListener("resize", resizeChart)
})

function getPictureUrlPrefix() {
getPicturePrefix().then(response => {
pictureUrlPrefix.value = response?.pictureUrlPrefix ?? ''
})
}

function getPortalAllList() {
allListPortalFrame().then(response => {
gateData.value = response.data
})
}

function getTableHeaderClass({row, column, rowIndex, columnIndex}): string {
return 'detail-table-header'
}

function getTableRowClass({row, column, rowIndex, columnIndex}): string {
return 'detail-table-row'
}

const resizeChart = () => {
map?.value?.resizeChart && map.value.resizeChart()
}

const getEntryCarList = (pageSize = defaultPageSize, pageNum = 1) => {
entryCarPageLoading.value = true;
listFlow({
...entryCarQueryParams.value,
entryOutProvince: "ENTRY"
}).then((response) => {
entryCarList.value = response.rows;
entryCarPageTotal.value = response.total;
entryCarPageLoading.value = false;

// Scroll to top
if (refEntryCar?.value && refEntryCar.value.bodyWrapper?.scrollTop) {
refEntryCar.value.bodyWrapper.scrollTop = 0;
}
}).catch((err) => {
console.error(err);
proxy?.$message.error('获取数据失败,刷新页面或者联系管理员!');
});
}

const getOutCarList = (pageSize = defaultPageSize, pageNum = 1) => {
outCarPageLoading.value = true;
listFlow({
...outCarQueryParams.value,
entryOutProvince: "OUT"
}).then((response) => {
outCarList.value = response.rows;
outCarPageTotal.value = response.total;
outCarPageLoading.value = false;

// Scroll to top
if (refOutCar?.value && refOutCar.value.bodyWrapper?.scrollTop) {
refOutCar.value.bodyWrapper.scrollTop = 0;
}
}).catch((err) => {
console.error(err);
proxy?.$message.error('获取数据失败,刷新页面或者联系管理员!');
});
}
const showCarInfo = (item) => {
if (item.portalCode && gateData.value?.map(gate => gate.portalCode)?.includes(item.portalCode)) {
let curItem = item
// 获取车辆的抓拍图
if (pictureUrlPrefix && pictureUrlPrefix.value !== '' && curItem.snapPictureId) {
getPicture(curItem.snapPictureId).then(result => {
curItem = {
...curItem,
image: pictureUrlPrefix.value + result.data.relativeAddress
}
}).catch((err) => {
proxy?.$message.error(err)
}).finally(() => {
let detail = deepClone(gateData.value.find(gate => gate.portalCode === curItem.portalCode))
detail = {
...detail,
car: curItem
}
map?.value?.showInfo && map.value.showInfo(detail)
})
} else {
let detail = deepClone(gateData.value.find(gate => gate.portalCode === item.portalCode))
detail = {
...detail,
car: item
}
map?.value?.showInfo && map.value.showInfo(detail)
}
} else {
proxy?.$message.info("没有对应的ETC门架信息")
}
}
</script>

<template>
<div class="home-count">
<div class="flex-box">
<div class="flex-map">
<CenterMap ref="map" image-background-size="200%"></CenterMap>
</div>
<div class="table-list">
<div class="portal-table-wrap">
<div class="portal-table">
<div class="corner" v-for="i in 4" :key="i" :class="'c' + i"></div>
<div class="portal-table-title">
<div class="title-left">
<img class="icon" src="/images/chart-icon-5.png" alt="出省信息"/>
<span class="title-content">出省信息</span>
</div>
</div>
<div class="portal-table-main">
<el-table
ref="refOutCar"
:data="outCarList"
v-loading="outCarPageLoading"
element-loading-spinner="el-icon-loading"
element-loading-background="rgba(0, 0, 0, 0.8)"
:header-cell-class-name="getTableHeaderClass"
:row-class-name="getTableRowClass"
height="calc(25vh - 10px)"
@row-click="showCarInfo"
>
<el-table-column label="门架编号" align="center" prop="portalCode" min-width="130px"/>
<el-table-column label="门架名称" align="center" prop="portalName"/>
<el-table-column label="车牌" align="center" prop="licensePlateNumber">
<template v-slot="scope">
<span class="car-number">{{ scope.row.licensePlateNumber }}</span>
</template>
</el-table-column>
<el-table-column label="通过时间" align="center" prop="passageTime" min-width="120px"/>
<el-table-column label="车型" align="center" prop="vehicleModel">
<template v-slot="scope">
<span>{{ consts.vehicleModels[scope.row.vehicleModel] }}</span>
</template>
</el-table-column>
<el-table-column label="车轴信息" align="center" prop="axleNumber" width="100px">
<template v-slot="scope">
<span>{{ scope.row.axleNumber }}轴</span>
</template>
</el-table-column>
</el-table>
<pagination
class="table-pagination"
v-show="outCarPageTotal>0"
:total="outCarPageTotal"
:page.sync="outCarQueryParams.pageNum"
:limit.sync="outCarQueryParams.pageSize"
:page-sizes="[10, 20, 50, 100]"
layout="total, sizes, prev, pager, next"
:pagerCount="5"
@pagination="getOutCarList"
/>
</div>
</div>
</div>
<div class="portal-table-wrap">
<div class="portal-table">
<div class="corner" v-for="i in 4" :key="i" :class="'c' + i"></div>
<div class="portal-table-title">
<div class="title-left">
<img class="icon" src="/images/chart-icon-4.png" alt="入省信息"/>
<span class="title-content">入省信息</span>
</div>
</div>
<div class="portal-table-main">
<el-table
ref="refEntryCar"
:data="entryCarList"
v-loading="entryCarPageLoading"
element-loading-spinner="el-icon-loading"
element-loading-background="rgba(0, 0, 0, 0.8)"
:header-cell-class-name="getTableHeaderClass"
:row-class-name="getTableRowClass"
height="calc(25vh - 10px)"
@row-click="showCarInfo"
>
<el-table-column label="门架编号" align="center" prop="portalCode" min-width="130px"/>
<el-table-column label="门架名称" align="center" prop="portalName"/>
<el-table-column label="车牌" align="center" prop="licensePlateNumber">
<template v-slot="scope">
<span class="car-number">{{ scope.row.licensePlateNumber }}</span>
</template>
</el-table-column>
<el-table-column label="通过时间" align="center" prop="passageTime" min-width="120px"/>
<el-table-column label="车型" align="center" prop="vehicleModel">
<template v-slot="scope">
<span>{{ consts.vehicleModels[scope.row.vehicleModel] }}</span>
</template>
</el-table-column>
<el-table-column label="车轴信息" align="center" prop="axleNumber" width="100px">
<template v-slot="scope">
<span>{{ scope.row.axleNumber }}轴</span>
</template>
</el-table-column>
</el-table>
<pagination
class="table-pagination"
v-show="entryCarPageTotal>0"
:total="entryCarPageTotal"
:page.sync="entryCarQueryParams.pageNum"
:limit.sync="entryCarQueryParams.pageSize"
:page-sizes="[10, 20, 50, 100]"
layout="total, sizes, prev, pager, next"
:pagerCount="5"
@pagination="getEntryCarList"
/>
</div>
</div>
</div>
</div>

</div>
</div>
</template>

<style lang="scss" scoped>
:deep(.el-table .el-table__header-wrapper th, .el-table th.el-table__cell) {
font-size: 15px;
color: #9ecad9;
border: none;
}

:deep(.el-table--enable-row-hover .el-table__body tr:hover > td) {
background-color: transparent;
color: #ffc300;
}

:deep(.el-table::before, .el-table--group::after, .el-table--border::after) {
background-color: transparent;
}

:deep(.detail-table-header) {
font-weight: bold;
}

:deep(.el-table tr) {
background-color: transparent;
}

:deep(.el-table th) {
background-color: transparent;
}

:deep(.detail-table-row) {
background-color: transparent;
color: #01d6ff;
font-size: 15px;
}

:deep(.el-table__row > td, .el-table th.is-leaf) {
border: none
}

:deep(.el-table, .el-table__expanded-cell) {
background-color: transparent;
}

:deep(.pagination-container) {
background-color: transparent;
}

.car-license-row {
color: #FFB956;
}


.home-count {
flex: 1;
padding: 15px 20px;

.car-number {
color: #FFB956;
}

.flex-box {
width: 100%;
display: flex;
justify-content: center;

.flex-map {
width: calc(100vh - 360px);
height: calc(100vh - 360px);
padding: 20px;
}

.table-list {
flex: 1;
max-width: 800px;
display: flex;
flex-direction: column;
}

.portal-table-wrap {
width: 100%;
height: calc(40vh - 30px);
position: relative;

.portal-table {
width: 100%;
display: flex;
flex-direction: column;
padding: 5px;
position: absolute;
background: url('/images/grid.png') left top;

.portal-table-main {
background-color: rgba(0, 0, 0, 0);
}

.portal-table-title {
flex: 1;
height: 54px;
padding: 5px 5px;
margin: 0;
font-size: 18px;
color: #fff;
display: flex;
align-items: center;
justify-content: space-between;

.title-left {
vertical-align: middle;
min-width: 120px;

.title-content {
vertical-align: middle;
}

.icon {
width: 30px;
height: 30px;
margin-right: 5px;
vertical-align: middle;

}
}

.table-pagination {
flex: 1;
background-color: rgba(0, 0, 0, 0);
}
}

:deep(.portal-search-form) {
.el-form-item--small .el-form-item__label {
color: #87b1b9;
}
}

.corner {
width: 12px;
height: 12px;
border: 3px solid #1084B6;
position: absolute;
z-index: 99;

&.c1 {
border-right: 0;
border-bottom: 0;
left: 0;
top: 0;
}

&.c2 {
border-left: 0;
border-bottom: 0;
right: 0;
top: 0;
}

&.c3 {
border-left: 0;
border-top: 0;
right: 0;
bottom: 0;
}

&.c4 {
border-top: 0;
border-right: 0;
left: 0;
bottom: 0;
}
}
}
}


}
}

</style>