Loading 載入
載入資料時在區域或整個視窗上蓋一層遮罩,中間顯示轉圈圖示和文字。有三種用法:寫成元件、呼叫 HuiLoading.service()、在元素上加 v-loading 指令。
引入
import { HuiLoading, vLoading } from '@nscode/handy-ui'
HuiLoading是元件,同時帶有服務方法HuiLoading.service(options)。服務方法也單獨導出為HuiLoadingService,兩者是同一個函式。vLoading是指令。全量註冊app.use(HandyUI)時會自動註冊成v-loading;按需引入時的註冊方式見下面的“v-loading 指令”一節。
基礎用法
元件用法:把 HuiLoading 放進要蓋住的區域,用 visible 控制顯示,可以反覆開關,關閉時有 0.3 秒的淡出。
遮罩是 position: absolute,上下左右都是 0,鋪滿最近的定位父元素。元件不會改父元素的樣式,所以父元素要自己寫上 position: relative,否則遮罩會鋪到更外層的定位元素上。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLoading } from '@nscode/handy-ui'
const loading = ref(false)
</script>
<template>
<div style="margin-bottom: 12px">
<HuiButton @click="loading = !loading">{{ loading ? '關閉' : '打開' }}</HuiButton>
</div>
<div style="position: relative; height: 120px">
<div style="padding: 16px">內容區域</div>
<HuiLoading :visible="loading" />
</div>
</template>
文字、圖示和背景
text顯示在圖示下方。spinner是@nscode/icons裡的圖示名,預設是'loading'。圖示會一直旋轉;名字寫錯時用預設的 loading 圖示。background是遮罩的背景色。不傳時是跟隨主題的底色(頁面背景色,90% 不透明),暗色主題下也是深色。
<script setup lang="ts">
import { HuiLoading } from '@nscode/handy-ui'
</script>
<template>
<div class="grid">
<div>
<div class="box"><HuiLoading visible /></div>
<p class="caption">預設</p>
</div>
<div>
<div class="box"><HuiLoading visible text="載入中" /></div>
<p class="caption">text</p>
</div>
<div>
<div class="box"><HuiLoading visible text="刷新中" spinner="refresh" /></div>
<p class="caption">spinner="refresh"</p>
</div>
<div>
<div class="box"><HuiLoading visible text="設定中" spinner="setting" /></div>
<p class="caption">spinner="setting"</p>
</div>
<div>
<div class="box"><HuiLoading visible text="名字寫錯" spinner="not-exist" /></div>
<p class="caption">圖示名寫錯時用預設圖示</p>
</div>
<div>
<div class="box">
<HuiLoading visible text="深色背景" spinner="refresh" background="rgba(0, 0, 0, 0.5)" />
</div>
<p class="caption">background</p>
</div>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
gap: var(--hui-spacing-3);
}
.box {
position: relative;
height: 120px;
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
border-radius: var(--hui-border-radius);
overflow: hidden;
}
.caption {
margin: var(--hui-spacing-2) 0 0;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
text-align: center;
}
</style>
全螢幕
元件加上 fullscreen 後,遮罩改成 position: fixed,鋪滿整個視窗,和元件寫在哪裡無關。下面的示例 2 秒後自動關閉。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLoading } from '@nscode/handy-ui'
const loading = ref(false)
const open = () => {
loading.value = true
window.setTimeout(() => (loading.value = false), 2000)
}
</script>
<template>
<HuiButton @click="open">全螢幕載入 2 秒</HuiButton>
<HuiLoading :visible="loading" fullscreen text="正在載入..." />
</template>
用方法控制
透過元件的 ref 呼叫 show()、close() 打開、關閉遮罩,visible 讀出遮罩目前是否在頁面上(淡出的 0.3 秒裡仍是 true)。這兩個方法不改 visible 屬性;只用方法控制時可以不傳 visible(預設 false),下面的示例就沒有傳。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLoading, HuiSpace } from '@nscode/handy-ui'
interface LoadingExpose {
show: () => void
close: () => void
visible: boolean
}
const loadingRef = ref<LoadingExpose>()
</script>
<template>
<HuiSpace wrap style="margin-bottom: 12px">
<HuiButton @click="loadingRef?.show()">show()</HuiButton>
<HuiButton @click="loadingRef?.close()">close()</HuiButton>
<span>visible:{{ loadingRef?.visible ?? false }}</span>
</HuiSpace>
<div style="position: relative; height: 120px">
<div style="padding: 16px">內容區域</div>
<HuiLoading ref="loadingRef" text="用方法打開的" />
</div>
</template>
服務方式
HuiLoading.service(options) 在程式碼裡打開遮罩,回傳一個實例,呼叫它的 close() 關閉。不用在模板裡寫元件,適合在請求函式、路由守衛裡用。
全螢幕載入
不傳 target 時遮罩掛在 document.body 上。要讓遮罩固定蓋住視窗、頁面捲動時不跟著走,傳 fullscreen: true。lock: true 會在載入期間鎖定頁面捲動,關閉後恢復。
下面用定時器模擬一個 2 秒的請求,請求結束後在 finally 裡關閉:
<script setup lang="ts">
import { HuiButton, HuiLoading } from '@nscode/handy-ui'
// 模擬一個 2 秒的請求
const fetchData = () => new Promise((resolve) => setTimeout(resolve, 2000))
const load = async () => {
const loading = HuiLoading.service({ fullscreen: true, lock: true, text: '正在載入...' })
try {
await fetchData()
} finally {
loading.close()
}
}
</script>
<template>
<HuiButton @click="load">全螢幕載入 2 秒</HuiButton>
</template>
局部載入
target 傳 DOM 元素或 CSS 選擇器,遮罩掛到這個元素裡面,鋪滿它。目標元素是 static 定位時,打開期間臨時改成 relative,最後一個掛在它上面的遮罩關閉後還原;打開期間目標元素上還會加上 hui-loading-parent 類。選擇器找不到元素時在主控台警告,改為掛到 document.body。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLoading, HuiSpace } from '@nscode/handy-ui'
const box = ref<HTMLElement>()
const byElement = () => {
const loading = HuiLoading.service({ target: box.value, text: 'target 傳元素' })
setTimeout(() => loading.close(), 2000)
}
const bySelector = () => {
const loading = HuiLoading.service({
target: '#loading-service-target',
text: 'target 傳選擇器'
})
setTimeout(() => loading.close(), 2000)
}
</script>
<template>
<HuiSpace wrap style="margin-bottom: 12px">
<HuiButton @click="byElement">傳元素,載入 2 秒</HuiButton>
<HuiButton @click="bySelector">傳選擇器,載入 2 秒</HuiButton>
</HuiSpace>
<div id="loading-service-target" ref="box" class="box">表格區域</div>
</template>
<style scoped>
.box {
height: 120px;
padding: 16px;
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
</style>
更新文字
實例的 update() 可以在載入過程中改文字、圖示和背景色。
<script setup lang="ts">
import { HuiButton, HuiLoading } from '@nscode/handy-ui'
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
const run = async () => {
const loading = HuiLoading.service({ fullscreen: true, text: '第 1 步:讀取資料' })
await wait(1000)
loading.update({ text: '第 2 步:保存', spinner: 'save' })
await wait(1000)
loading.update({ text: '第 3 步:完成', spinner: 'check', background: 'rgba(0, 0, 0, 0.6)' })
await wait(800)
loading.close()
}
</script>
<template>
<HuiButton @click="run">分 3 步載入</HuiButton>
</template>
全螢幕單例
全螢幕的遮罩(fullscreen: true,或者沒傳 target、掛在 document.body 上)同一時間只有一個。已經有一個打開著(包括正在淡出的)時再呼叫 HuiLoading.service(),拿到的是同一個實例:文字、圖示、背景按這次的配置更新,正在進行的關閉會被取消。任何一處呼叫 close() 都會把它關掉。
掛到其他元素上的遮罩不是單例,每次呼叫都新建一個,要各自關閉。
下面重複執行會替換本例的兩級定時器;卸載只取消後續動作,不關閉可能已被新頁面重用的全螢幕實例。
<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue'
import { HuiButton, HuiLoading } from '@nscode/handy-ui'
// 生命周期示例:Loading全螢幕單例
const result = ref('')
let alive = true
let version = 0
let updateTimer: ReturnType<typeof setTimeout> | undefined
let closeTimer: ReturnType<typeof setTimeout> | undefined
const clearTimers = () => {
clearTimeout(updateTimer)
clearTimeout(closeTimer)
updateTimer = undefined
closeTimer = undefined
}
onBeforeUnmount(() => {
alive = false
version++
// 只取消本示例尾部,不關閉可能已被新頁面重用的全螢幕單例。
clearTimers()
})
const run = () => {
if (!alive) return
const request = ++version
clearTimers()
const first = HuiLoading.service({ fullscreen: true, text: '第一次呼叫' })
updateTimer = setTimeout(() => {
updateTimer = undefined
if (!alive || request !== version) return
const second = HuiLoading.service({ fullscreen: true, text: '第二次呼叫:還是同一個遮罩' })
result.value = `兩次呼叫拿到的是同一個實例:${first === second}`
closeTimer = setTimeout(() => {
closeTimer = undefined
if (alive && request === version) second.close()
}, 1500)
}, 1000)
}
</script>
<template>
<HuiButton @click="run">連續呼叫兩次</HuiButton>
<p style="margin-top: 12px">{{ result || '點按鈕看看' }}</p>
</template>
v-loading 指令
指令的值是布林值,為 true 時在元素裡蓋上遮罩,為 false 時淡出後移除。元素是 static 定位時,打開期間臨時改成 relative,關閉後還原。
全量註冊 app.use(HandyUI) 時指令已經註冊好了。按需引入時有兩種註冊方式:
- 全域註冊:
app.directive('loading', vLoading)。 - 在
<script setup>裡import { vLoading } from '@nscode/handy-ui',模板裡直接用v-loading。
文字、圖示、背景色透過元素上的屬性設定,不寫時和元件的預設值一樣:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, vLoading } from '@nscode/handy-ui'
const loading = ref(false)
</script>
<template>
<div style="margin-bottom: 12px">
<HuiButton @click="loading = !loading">{{ loading ? '關閉' : '打開' }}</HuiButton>
</div>
<div class="grid">
<div v-loading="loading" class="box">不寫屬性</div>
<div
v-loading="loading"
element-loading-text="拼命載入中"
element-loading-spinner="refresh"
element-loading-background="rgba(0, 0, 0, 0.6)"
class="box"
>
寫了文字、圖示和背景色
</div>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: var(--hui-spacing-3);
}
.box {
height: 120px;
padding: 16px;
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
</style>
element-loading-text、element-loading-spinner、element-loading-background 在所在元件每次更新時重新讀取,綁定的值變了,遮罩上的文字跟著變。element-loading-fullscreen="true" 讓遮罩鋪滿視窗,它只在遮罩建立時讀取。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, vLoading } from '@nscode/handy-ui'
const loading = ref(false)
const percent = ref(0)
const fullscreen = ref(false)
const start = () => {
percent.value = 0
loading.value = true
const timer = setInterval(() => {
percent.value += 20
if (percent.value >= 100) {
clearInterval(timer)
loading.value = false
}
}, 400)
}
const openFullscreen = () => {
fullscreen.value = true
setTimeout(() => (fullscreen.value = false), 2000)
}
</script>
<template>
<HuiSpace wrap style="margin-bottom: 12px">
<HuiButton :disabled="loading" @click="start">開始載入</HuiButton>
<HuiButton @click="openFullscreen">全螢幕載入 2 秒</HuiButton>
</HuiSpace>
<div v-loading="loading" :element-loading-text="`已載入 ${percent}%`" class="box">內容區域</div>
<div
v-loading="fullscreen"
element-loading-fullscreen="true"
element-loading-text="全螢幕載入中,2 秒後關閉"
/>
</template>
<style scoped>
.box {
height: 120px;
padding: 16px;
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
</style>
Props
元件用法的屬性。
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| visible | boolean | false | 是否顯示。可以反覆開關,關閉時淡出 0.3 秒。只用 show()、close() 控制時可以不傳 |
| text | string | — | 圖示下方的文字 |
| spinner | string | 'loading' | 圖示名,取 @nscode/icons 裡的圖示;寫錯時用預設的 loading 圖示 |
| background | string | — | 遮罩背景色。不傳時跟隨主題 |
| fullscreen | boolean | false | 為 true 時遮罩 fixed 鋪滿視窗;為 false 時 absolute 鋪滿最近的定位父元素 |
元件暴露的方法
透過元件的 ref 呼叫:
| 名稱 | 類型 | 說明 |
|---|---|---|
| show | () => void | 顯示遮罩。正在淡出時呼叫會取消關閉 |
| close | () => void | 淡出後隱藏遮罩。已經隱藏或正在淡出時呼叫不做任何事 |
| visible | boolean | 遮罩目前是否在頁面上(淡出過程中仍是 true),只用來讀取 |
show()、close() 不會改 visible 屬性。之後 visible 屬性再變化時,以屬性的新值為準。
HuiLoading.service 配置項
| 配置項 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| target | HTMLElement | string | document.body | 遮罩掛到哪個元素上。字串按 CSS 選擇器查找,找不到時警告並改用 document.body |
| text | string | — | 圖示下方的文字 |
| spinner | string | 'loading' | 圖示名,寫錯時用預設的 loading 圖示 |
| background | string | — | 遮罩背景色。不傳時跟隨主題 |
| fullscreen | boolean | false | 為 true 時遮罩 fixed 鋪滿視窗 |
| lock | boolean | false | 載入期間鎖定頁面捲動,關閉後恢復。只對全螢幕遮罩(fullscreen: true 或掛在 body 上)生效 |
HuiLoading.service 回傳值
回傳 LoadingInstance:
| 方法 | 類型 | 說明 |
|---|---|---|
| close | () => void | 淡出 0.3 秒後移除遮罩,還原目標元素的定位和頁面捲動。重複呼叫只關一次 |
| update | (options: Partial<LoadingProps>) => void | 更新 text、spinner、background、fullscreen;傳 visible: true 可以取消正在進行的關閉 |
v-loading 屬性
| 屬性 | 說明 |
|---|---|
| element-loading-text | 圖示下方的文字。所在元件每次更新時重新讀取,改了會跟著變 |
| element-loading-spinner | 圖示名,不寫時是 'loading'。所在元件每次更新時重新讀取 |
| element-loading-background | 遮罩背景色,不寫時跟隨主題。所在元件每次更新時重新讀取 |
| element-loading-fullscreen | 寫 "true" 時遮罩 fixed 鋪滿視窗。只在遮罩建立時讀取,之後改了不生效 |
類型
import type {
LoadingOptions,
LoadingInstance,
LoadingProps,
LoadingDirectiveBinding,
LoadingServiceFn
} from '@nscode/handy-ui'
interface LoadingOptions {
target?: HTMLElement | string
text?: string
spinner?: string
background?: string
fullscreen?: boolean
lock?: boolean
}
interface LoadingInstance {
close: () => void
update: (options: Partial<LoadingProps>) => void
}
interface LoadingProps {
text?: string
spinner?: string
background?: string
fullscreen?: boolean
visible: boolean
}
interface LoadingDirectiveBinding {
value: boolean
oldValue?: boolean
arg?: string
modifiers?: Record<string, boolean>
}
type LoadingServiceFn = (options: LoadingOptions) => LoadingInstance
另外導出了 HuiLoadingComponent,和 HuiLoading 是同一個元件。