2020-08-13 15:18:26 +08:00
## Loading
Show animation while loading data.
### Loading inside a container
Displays animation in a container (such as a table) while loading data.
2020-10-16 11:14:34 +08:00
:::demo Element Plus provides two ways to invoke Loading: directive and service. For the custom directive `v-loading` , you just need to bind a `boolean` value to it. By default, the loading mask will append to the element where the directive is used. Adding the `body` modifier makes the mask append to the body element.
2020-08-13 15:18:26 +08:00
```html
< template >
2021-09-04 19:29:28 +08:00
< el-table v-loading = "loading" :data = "tableData" style = "width: 100%" >
< el-table-column prop = "date" label = "Date" width = "180" > < / el-table-column >
< el-table-column prop = "name" label = "Name" width = "180" > < / el-table-column >
< el-table-column prop = "address" label = "Address" > < / el-table-column >
2020-08-13 15:18:26 +08:00
< / el-table >
< / template >
< style >
body {
margin: 0;
}
< / style >
< script >
export default {
data() {
return {
2021-09-04 19:29:28 +08:00
tableData: [
{
date: '2016-05-02',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
{
date: '2016-05-04',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
{
date: '2016-05-01',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
],
loading: true,
}
},
}
2020-08-13 15:18:26 +08:00
< / script >
2021-06-10 23:55:09 +08:00
<!--
< setup >
import { defineComponent, reactive, toRefs } from 'vue';
export default defineComponent({
setup() {
const state = reactive({
tableData: [
{
date: '2016-05-02',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
{
date: '2016-05-04',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
{
date: '2016-05-01',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
],
loading: true,
});
return {
...toRefs(state),
};
},
});
< / setup >
-->
2020-08-13 15:18:26 +08:00
```
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
:::
### Customization
You can customize loading text, loading spinner and background color.
2021-07-12 18:13:19 +08:00
:::demo Add attribute `element-loading-text` to the element on which `v-loading` is bound, and its value will be displayed under the spinner. Similarly, the `element-loading-spinner` , `element-loading-background` , and `element-loading-svg` attributes are used to set the icon class name, background color value, and loading icon, respectively.
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
```html
< template >
< el-table
v-loading="loading"
element-loading-text="Loading..."
element-loading-spinner="el-icon-loading"
element-loading-background="rgba(0, 0, 0, 0.8)"
:data="tableData"
2021-09-04 19:29:28 +08:00
style="width: 100%"
>
< el-table-column prop = "date" label = "Date" width = "180" > < / el-table-column >
< el-table-column prop = "name" label = "Name" width = "180" > < / el-table-column >
< el-table-column prop = "address" label = "Address" > < / el-table-column >
2020-08-13 15:18:26 +08:00
< / el-table >
2021-07-12 18:13:19 +08:00
< el-table
v-loading="loading"
:element-loading-svg="svg"
class="custom-loading-svg"
element-loading-svg-view-box="-10, -10, 50, 50"
:data="tableData"
2021-09-04 19:29:28 +08:00
style="width: 100%"
>
< el-table-column prop = "date" label = "Date" width = "180" > < / el-table-column >
< el-table-column prop = "name" label = "Name" width = "180" > < / el-table-column >
< el-table-column prop = "address" label = "Address" > < / el-table-column >
2021-07-12 18:13:19 +08:00
< / el-table >
2020-08-13 15:18:26 +08:00
< / template >
< script >
export default {
data() {
return {
2021-09-04 19:29:28 +08:00
tableData: [
{
date: '2016-05-02',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
{
date: '2016-05-04',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
{
date: '2016-05-01',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
],
2021-07-12 18:13:19 +08:00
loading: true,
svg: `
< path class = "path" d = "
M 30 15
L 28 17
M 25.61 25.61
A 15 15, 0, 0, 1, 15 30
A 15 15, 0, 1, 1, 27.99 7.5
L 15 15
" style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
`,
2021-09-04 19:29:28 +08:00
}
},
}
2020-08-13 15:18:26 +08:00
< / script >
2021-06-10 23:55:09 +08:00
<!--
< setup >
import { defineComponent, reactive, toRefs } from 'vue';
export default defineComponent({
setup() {
const state = reactive({
tableData: [
{
date: '2016-05-02',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
{
date: '2016-05-04',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
{
date: '2016-05-01',
name: 'John Smith',
address: 'No.1518, Jinshajiang Road, Putuo District',
},
],
loading: true,
2021-07-12 18:13:19 +08:00
svg: `
< path class = "path" d = "
M 30 15
L 28 17
M 25.61 25.61
A 15 15, 0, 0, 1, 15 30
A 15 15, 0, 1, 1, 27.99 7.5
L 15 15
" style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
`,
2021-06-10 23:55:09 +08:00
});
return {
...toRefs(state),
};
},
});
< / setup >
-->
2020-08-13 15:18:26 +08:00
```
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
:::
2021-07-12 18:13:19 +08:00
:::warning
Although the `element-loading-svg` attribute supports incoming HTML fragments, it is very dangerous to dynamically render arbitrary HTML on the website, because it is easy to cause [XSS attack ](https://en.wikipedia.org/wiki/Cross-site_scripting ). Please make sure that the content of `element-loading-svg` is trustworthy. **Never** assign user-submitted content to the `element-loading-svg` attribute.
:::
2020-08-13 15:18:26 +08:00
### Full screen loading
Show a full screen animation while loading data.
:::demo When used as a directive, a full screen Loading requires the `fullscreen` modifier, and it will be appended to body. In this case, if you wish to disable scrolling on body, you can add another modifier `lock` . When used as a service, Loading will be full screen by default.
```html
< template >
< el-button
type="primary"
@click ="openFullScreen1"
2021-09-04 19:29:28 +08:00
v-loading.fullscreen.lock="fullscreenLoading"
>
2020-08-13 15:18:26 +08:00
As a directive
< / el-button >
2021-09-04 19:29:28 +08:00
< el-button type = "primary" @click =" openFullScreen2 " > As a service </ el-button >
2020-08-13 15:18:26 +08:00
< / template >
< script >
export default {
data() {
return {
2021-09-04 19:29:28 +08:00
fullscreenLoading: false,
2020-08-13 15:18:26 +08:00
}
},
methods: {
openFullScreen1() {
2021-09-04 19:29:28 +08:00
this.fullscreenLoading = true
2020-08-13 15:18:26 +08:00
setTimeout(() => {
2021-09-04 19:29:28 +08:00
this.fullscreenLoading = false
}, 2000)
2020-08-13 15:18:26 +08:00
},
openFullScreen2() {
const loading = this.$loading({
lock: true,
text: 'Loading',
spinner: 'el-icon-loading',
2021-09-04 19:29:28 +08:00
background: 'rgba(0, 0, 0, 0.7)',
})
2020-08-13 15:18:26 +08:00
setTimeout(() => {
2021-09-04 19:29:28 +08:00
loading.close()
}, 2000)
},
},
2020-08-13 15:18:26 +08:00
}
< / script >
2021-06-10 23:55:09 +08:00
<!--
< setup >
import { defineComponent, ref } from 'vue';
import { ElLoading } from 'element-plus';
export default defineComponent({
setup() {
const fullscreenLoading = ref(false);
const openFullScreen1 = () => {
fullscreenLoading.value = true;
setTimeout(() => {
fullscreenLoading.value = false;
}, 2000);
};
const openFullScreen2 = () => {
const loading = ElLoading.service({
lock: true,
text: 'Loading',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)',
});
setTimeout(() => {
loading.close();
}, 2000);
};
return {
fullscreenLoading,
openFullScreen1,
openFullScreen2,
};
},
});
< / setup >
-->
2020-08-13 15:18:26 +08:00
```
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
:::
### Service
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
You can also invoke Loading with a service. Import Loading service:
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
```javascript
2021-09-04 19:29:28 +08:00
import { ElLoading } from 'element-plus'
2020-08-13 15:18:26 +08:00
```
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
Invoke it:
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
```javascript
2021-09-04 19:29:28 +08:00
ElLoading.service(options)
2020-08-13 15:18:26 +08:00
```
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
The parameter `options` is the configuration of Loading, and its details can be found in the following table. `LoadingService` returns a Loading instance, and you can close it by invoking its `close` method:
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
```javascript
2021-09-04 19:29:28 +08:00
let loadingInstance = ElLoading.service(options)
this.$nextTick(() => {
// Loading should be closed asynchronously
loadingInstance.close()
})
2020-08-13 15:18:26 +08:00
```
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
Note that in this case the full screen Loading is singleton. If a new full screen Loading is invoked before an existing one is closed, the existing full screen Loading instance will be returned instead of actually creating another Loading instance:
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
```javascript
2021-09-04 19:29:28 +08:00
let loadingInstance1 = ElLoading.service({ fullscreen: true })
let loadingInstance2 = ElLoading.service({ fullscreen: true })
console.log(loadingInstance1 === loadingInstance2) // true
2020-08-13 15:18:26 +08:00
```
2021-09-04 19:29:28 +08:00
2020-08-13 15:18:26 +08:00
Calling the `close` method on any one of them can close this full screen Loading.
2020-11-23 16:28:06 +08:00
If Element Plus is imported entirely, a globally method `$loading` will be registered to `app.config.globalProperties` . You can invoke it like this: `this.$loading(options)` , and it also returns a Loading instance.
2020-08-13 15:18:26 +08:00
### Options
2021-09-04 19:29:28 +08:00
| Attribute | Description | Type | Accepted Values | Default |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------- | --------------- | ------------- |
| target | the DOM node Loading needs to cover. Accepts a DOM object or a string. If it's a string, it will be passed to `document.querySelector` to get the corresponding DOM node | object/string | — | document.body |
| body | same as the `body` modifier of `v-loading` | boolean | — | false |
| fullscreen | same as the `fullscreen` modifier of `v-loading` | boolean | — | true |
| lock | same as the `lock` modifier of `v-loading` | boolean | — | false |
| text | loading text that displays under the spinner | string | — | — |
| spinner | class name of the custom spinner | string | — | — |
| background | background color of the mask | string | — | — |
| customClass | custom class name for Loading | string | — | — |
2021-04-06 11:41:02 +08:00
### Directives
2021-09-04 19:29:28 +08:00
| Name | Description | Type |
| -------------------------- | -------------------------------------------- | ------- |
| v-loading | show animation while loading data | boolean |
| element-loading-text | loading text that displays under the spinner | string |
| element-loading-spinner | class name of the custom spinner | string |
| element-loading-background | background color of the mask | string |