로딩 플러그인
로딩 플러그인 (Loading Plugin)
Loading은 앱 콘텐츠 위에 스피너가 있는 오버레이를 표시해서, 사용자에게 백그라운드 작업이 진행 중임을 알리는 데 사용할 수 있는 기능이에요. 전역 백그라운드 작업을 위해 Pages 안에 복잡한 로직을 추가할 필요가 없어요.
출처: 문서
본문
Loading API
Props (속성)
isActive(boolean, optional, reactive) Loading이 활성인가요?
Methods (메서드)
show(opts?: object): Function활성화하고 표시해요 파라미터:opts(object, optional) 모든 props는 선택 사항이에요 객체 형태:delay(number, optional) 표시하기 전에 몇 밀리초 기다려요; 예를 들어 100ms 동안만 표시했다가 숨기는 것은 가치가 없으므로, 상당한 시간이 걸릴 프로세스라고 확신할 때까지 기다리세요message(string, optional) 표시할 메시지 예시:'Processing your request'group(string, optional) 로딩 그룹 이름 예시:'some-api-call'html(boolean, optional) 메시지를 HTML로 렌더링해요; XSS 공격으로 이어질 수 있으므로 메시지를 먼저 소독하세요boxClass(string, optional) 콘텐츠를 감싼 요소의 커스텀 클래스 예시:'bg-amber text-black','q-pa-xl'spinnerSize(number, optional) 스피너 크기(픽셀 단위)spinnerColor(string, optional) Quasar 색상 팔레트의 스피너 색상 이름 예시:'primary','teal','teal-10'messageColor(string, optional) Quasar 색상 팔레트의 텍스트 색상 이름 예시:'primary','teal','teal-10'backgroundColor(string, optional) Quasar 색상 팔레트의 배경 색상 이름 예시:'primary','teal','teal-10'spinner(Component, optional) QSpinners 중 하나customClass(string, optional) 컴포넌트를 쉽게 커스터마이즈하는 CSS 클래스 추가 예시:'my-class'ignoreDefaults(boolean, optional) 이 인스턴스에만 기본 구성(setDefaults()로 설정된)을 무시해요 반환:Function이 함수를 파라미터 없이 호출하면 그룹을 숨겨요; Object 파라미터 하나로 호출하면 Loading 그룹을 업데이트해요(지정된 속성은 그룹 속성과 얕게 병합됨; 업데이트 중에는 group을 변경할 수 없고 무시된다는 점 참고) 함수 시그니처:(props?: object) => void파라미터:props(object, optional) 그룹 속성에 얕게 병합될 Loading 속성; ('group' 제외한 객체 속성은 'show()'의 'opts' 파라미터 참고)
hide(group?: string): void숨겨요 파라미터:group(string, optional) 모든 그룹을 숨기는 대신 숨길 선택적 Loading 그룹 이름 예시:'some-api-call'
setDefaults(opts: object): void옵션을 기본값에 병합해요 파라미터:opts(object, required) 정의하려는 하위 속성을 선택하세요 객체 형태:delay(number, optional) 표시하기 전에 몇 밀리초 기다려요; 예를 들어 100ms 동안만 표시했다가 숨기는 것은 가치가 없으므로, 상당한 시간이 걸릴 프로세스라고 확신할 때까지 기다리세요message(string, optional) 표시할 메시지 예시:'Processing your request'group(string, optional), 기본값'__default_quasar_group__'기본 Loading 그룹 이름 예시:'default-group-name'spinnerSize(number, optional) 스피너 크기(픽셀 단위)spinnerColor(string, optional) Quasar 색상 팔레트의 스피너 색상 이름 예시:'primary','teal','teal-10'messageColor(string, optional) Quasar 색상 팔레트의 텍스트 색상 이름 예시:'primary','teal','teal-10'backgroundColor(string, optional) Quasar 색상 팔레트의 배경 색상 이름 예시:'primary','teal','teal-10'spinner(Component, optional) QSpinners 중 하나customClass(string, optional) 컴포넌트를 쉽게 커스터마이즈하는 CSS 클래스 추가 예시:'my-class'
Vue 주입 (Vue Injection)
$q.loading을 통해 접근할 수 있어요(예: Options API에서는 this.$q.loading, Composition API에서는 useQuasar().loading).
quasar.config.js 옵션 (Options)
설정 키: framework.config.loading (object)
delay(number, optional) 표시하기 전에 몇 밀리초 기다려요; 예를 들어 100ms 동안만 표시했다가 숨기는 것은 가치가 없으므로, 상당한 시간이 걸릴 프로세스라고 확신할 때까지 기다리세요 예시:400message(string, optional) 표시할 메시지 예시:'Processing your request'group(string, optional), 기본값'__default_quasar_group__'기본 Loading 그룹 이름 예시:'default-group-name'html(boolean, optional) 메시지를 HTML로 강제 렌더링해요; XSS 공격으로 이어질 수 있으므로 콘텐츠를 먼저 소독하세요boxClass(string, optional) 콘텐츠를 감싼 요소의 커스텀 클래스 예시:'bg-amber text-black','q-pa-xl'spinnerSize(number, optional) 스피너 크기(픽셀 단위)spinnerColor(string, optional) Quasar 색상 팔레트의 스피너 색상 이름 예시:'primary','teal','teal-10'messageColor(string, optional) Quasar 색상 팔레트의 텍스트 색상 이름 예시:'primary','teal','teal-10'backgroundColor(string, optional) Quasar 색상 팔레트의 배경 색상 이름 예시:'primary','teal','teal-10'spinner(Component, optional) QSpinners 중 하나 quasar.config 파일 유형:string예시:QSpinnerAudiocustomClass(string, optional) 컴포넌트를 쉽게 커스터마이즈하려고 컨테이너 요소에 CSS 클래스 추가 예시:'my-class'
설치 (Installation)
quasar.config.js에 추가해요:
framework: {
plugins: [
'Loading'
],
config: {
loading: { /* look at QuasarConfOptions from the API card */ }
}
}
사용법 (Usage)
Loading은 자신을 표시하는 데 지연(500ms)을 사용해서, 빠른 작업이 화면을 깜빡이게 하지 않도록 해요. 이는 사용자가 무슨 일이 일어나는지 볼 기회도 없이 진행 스피너를 표시했다가 빠르게 숨기는 일이 생기기 때문이에요. 표시 전 지연이 혼란을 없애요.
Vue 컴포넌트 안에서:
import { useQuasar } from 'quasar'
setup () {
const $q = useQuasar()
$q.loading.show({
delay: 400 // ms
})
$q.loading.hide()
}
Vue 컴포넌트 밖에서:
import {
Loading,
// optional!, for example below
// with custom spinner
QSpinnerGears
} from 'quasar'
// default options
Loading.show()
// fully customizable
Loading.show({
spinner: QSpinnerGears
// other props
})
Loading.hide()
기본 옵션 (Default options)
<template>
<q-btn color="purple" @click="showLoading" label="Show Loading" />
</template>
<script setup>
import { useQuasar } from 'quasar'
import { onBeforeUnmount } from 'vue'
const $q = useQuasar()
let timer
onBeforeUnmount(() => {
if (timer !== void 0) {
clearTimeout(timer)
$q.loading.hide()
}
})
function showLoading() {
$q.loading.show()
// hiding in 2s
timer = setTimeout(() => {
$q.loading.hide()
timer = void 0
}, 2000)
}
</script>
커스터마이징 (Customization)
예시 "메시지 포함":
<template>
<q-btn color="teal" @click="showLoading" label="Show Loading" />
</template>
<script setup>
import { useQuasar } from 'quasar'
import { onBeforeUnmount } from 'vue'
const $q = useQuasar()
let timer
onBeforeUnmount(() => {
if (timer !== void 0) {
clearTimeout(timer)
$q.loading.hide()
}
})
function showLoading() {
$q.loading.show({
message: 'Some important process is in progress. Hang on...'
})
// hiding in 3s
timer = setTimeout(() => {
$q.loading.hide()
timer = void 0
}, 3000)
}
</script>
예시 "커스터마이즈된 박스":
<template>
<q-btn color="purple" @click="showLoading" label="Show Loading" />
</template>
<script setup>
import { useQuasar } from 'quasar'
import { onBeforeUnmount } from 'vue'
const $q = useQuasar()
let timer
onBeforeUnmount(() => {
if (timer !== void 0) {
clearTimeout(timer)
$q.loading.hide()
}
})
function showLoading() {
$q.loading.show({
message: 'Doing something. Please wait...',
boxClass: 'bg-grey-2 text-grey-9',
spinnerColor: 'primary'
})
// hiding in 3s
timer = setTimeout(() => {
$q.loading.hide()
timer = void 0
}, 3000)
}
</script>
예시 "커스터마이즈됨":
<template>
<q-btn color="red" @click="showLoading" label="Show Loading" />
</template>
<script setup>
import { QSpinnerFacebook, useQuasar } from 'quasar'
import { onBeforeUnmount } from 'vue'
const $q = useQuasar()
let timer
onBeforeUnmount(() => {
if (timer !== void 0) {
clearTimeout(timer)
$q.loading.hide()
}
})
function showLoading() {
$q.loading.show({
spinner: QSpinnerFacebook,
spinnerColor: 'yellow',
spinnerSize: 140,
backgroundColor: 'purple',
message: 'Some important process is in progress. Hang on...',
messageColor: 'black'
})
// hiding in 3s
timer = setTimeout(() => {
$q.loading.hide()
timer = void 0
}, 3000)
}
</script>
예시 "표시 후 변경 (Show and Change)":
<template>
<q-btn color="primary" @click="showLoading" label="Show Loading" />
</template>
<script setup>
import { QSpinnerGears, useQuasar } from 'quasar'
import { onBeforeUnmount } from 'vue'
const $q = useQuasar()
let timer
onBeforeUnmount(() => {
if (timer !== void 0) {
clearTimeout(timer)
$q.loading.hide()
}
})
function showLoading() {
$q.loading.show({
message: 'First message. Gonna change it in 3 seconds...'
})
timer = setTimeout(() => {
$q.loading.show({
spinner: QSpinnerGears,
spinnerColor: 'red',
messageColor: 'black',
backgroundColor: 'yellow',
message: 'Updated message'
})
timer = setTimeout(() => {
$q.loading.hide()
timer = void 0
}, 2000)
}, 2000)
}
</script>
콘텐츠 소독 (Content sanitization)
예시 "안전하지 않지만 소독된 메시지":
<template>
<q-btn color="teal" @click="showLoading" label="Show Loading (Sanitized)" />
</template>
<script setup>
import { useQuasar } from 'quasar'
import { onBeforeUnmount } from 'vue'
const $q = useQuasar()
let timer
onBeforeUnmount(() => {
if (timer !== void 0) {
clearTimeout(timer)
$q.loading.hide()
}
})
function showLoading() {
$q.loading.show({
message:
'Some important <b>process</b> is in progress.<br><span class="text-amber text-italic">Please wait...</span>',
html: true
})
// hiding in 3s
timer = setTimeout(() => {
$q.loading.hide()
timer = void 0
}, 3000)
}
</script>
병렬의 여러 그룹 (Multiple groups in parallel)
[!TIP] 병렬로 발생하는 여러 프로세스가 있다면 Loading 인스턴스를 그룹화해서 그룹별로(개별적으로) Loading 상태를 관리할 수 있어요.
각 Loading 인스턴스를 생성할 때 group 속성을 지정하면, 반환된 함수를 사용해 업데이트하거나 숨길 수 있어요.
당연히 한 번에 한 그룹만 표시할 수 있으므로, 생성된 순서가 표시 우선순위를 결정해요(마지막이 이전 것보다 우선; LIFO).
예시 "여러 그룹":
<template>
<q-btn
color="purple"
@click="showMultipleGroups"
label="Show Multiple Groups"
/>
</template>
<script setup>
import { useQuasar } from 'quasar'
import { onBeforeUnmount } from 'vue'
const $q = useQuasar()
let timer
onBeforeUnmount(() => {
if (timer !== void 0) {
clearTimeout(timer)
$q.loading.hide()
}
})
function showMultipleGroups() {
const first = $q.loading.show({
group: 'first',
message: 'This is first group',
spinnerColor: 'amber',
messageColor: 'amber'
})
// hiding in 2s
timer = setTimeout(() => {
const second = $q.loading.show({
group: 'second',
message: 'This is second group'
})
timer = setTimeout(() => {
// we hide second one (only); but we will still have the first one active
second()
// we update 'first' group message (just highlighting how it can be done);
// note that updating here is not required to show the remaining 'first' group
first({
message: 'We hid the second group and updated the first group message'
})
timer = setTimeout(() => {
// we hide all that may be showing
$q.loading.hide()
timer = void 0
}, 2000)
}, 2000)
}, 1500)
}
</script>
반환 함수를 사용해 그룹을 표시/업데이트/숨기거나, 그냥 Loading.show({ group: '..group_name..', ... }) 또는 Loading.hide('..group_name..')을 호출할 수 있어요.
다음 두 방식은 완전히 동일해요(그리고 그 사이에서 호출을 섞을 수도 있어요):
/**
* First way
*/
// we spawn the group
const myLoadingGroup = Loading.show({
group: 'my-group',
message: 'Some message'
})
// with params, so we update this group
myLoadingGroup({ message: 'Second message' })
// no params, so we instruct Quasar to hide the group
myLoadingGroup()
/**
* Second, equivalent way
*/
// we spawn the group
Loading.show({
group: 'my-group',
message: 'Some message'
})
// we update the group (in this case we need to specify the group name)
Loading.show({
group: 'my-group'
message: 'Second message'
})
// we hide this specific group
Loading.hide('my-group')
[!WARNING] 파라미터 없이
Loading.hide()를 호출하면 모든 그룹을 숨긴다는 점을 기억하세요. 그러니 그룹을 사용한다면 항상 그룹 이름으로 hide()를 호출하는 게 좋아요.
기본값 설정 (Setting Up Defaults)
매번 지정하는 대신 기본값을 설정하고 싶다면, quasar.config 파일 > framework > config > loading: {...}을 사용하거나 Loading.setDefaults({...}) 또는 $q.loading.setDefaults({...})을 호출할 수 있어요.
// quasar.config file
framework: {
config: {
loading: {
// the config file cannot import components,
// so a spinner is referred to by its name
spinner: 'QSpinnerGears'
// ...other Loading options
}
}
}
더 알아보기 (Learn more)
Loading 플러그인은 $q.loading.show({...})으로 앱 콘텐츠 위에 스피너 오버레이를 표시하고 $q.loading.hide()으로 숨기는 전역 로딩 표시기예요. message, spinner/spinnerSize/spinnerColor, boxClass, backgroundColor 등으로 커스터마이즈하고, group으로 병렬 프로세스를 개별 관리할 수 있어요(반환 함수로 업데이트/숨김). 기본 500ms 지연으로 빠른 작업의 화면 깜빡임을 방지하고, setDefaults()/quasar.config loading 설정으로 기본값을 지정할 수 있어요. html: true는 XSS 위험이 있으므로 반드시 콘텐츠를 소독하세요.