程序员scholar 程序员scholar
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
npm

(进入注册为作者充电)

  • 快速入手

  • 基础组件

  • 表单组件

  • 数据展示组件

    • 表格(Table)
    • 表格 (curd) 封装
    • Tag (标签)
    • 进度条(Progress)
    • 树形控件(Tree)
    • 分页组件 (Pagination)
    • 标记组件 (Badge)
    • 头像组件(Avatar)
    • 骨架屏组件 (Skeleton)
    • 空状态组件 (Empty)
      • 1. 基本用法
      • 2. Empty 属性
      • 3. Empty 插槽
      • 4. 常用示例
        • 自定义图片和描述
        • 自定义图片大小
        • 插槽自定义内容
        • 完全自定义内容
      • 5. 仿B站404页面
    • 描述列表组件 (Descriptions)
    • 结果组件 (Result)
    • 统计数值组件 (Statistic)
  • 反馈组件

  • 导航组件

  • 其他组件

  • Element-UI
  • 数据展示组件
scholar
2024-08-12
目录

空状态组件 (Empty)

# 空状态组件 (Empty)

Element UI 的 Empty 组件用于在数据为空或无法加载内容时显示占位提示。它提供了灵活的自定义选项,包括图片、描述文字和底部内容。

提示

Empty 组件官方文档:https://element.eleme.cn/#/zh-CN/component/empty (opens new window)

# 1. 基本用法

基本语法:在 Vue 组件中使用 <el-empty> 标签可以直接创建一个空状态提示。可以通过 image、image-size 和 description 属性自定义图片和描述文字。

<template>
  <el-empty description="暂无数据"></el-empty>
</template>

<script>
export default {
  // 此处可以根据需要添加逻辑
};
</script>
1
2
3
4
5
6
7
8
9
  • description 属性:用于显示自定义的描述文字,当不指定时,会显示默认的提示文字 "暂无数据"。
  • image-20240810114702815

# 2. Empty 属性

Empty 组件提供了几个属性,用于自定义显示内容,如图片、描述文字和图片的大小。

参数 说明 类型 可选值 默认值
image 图片地址 string — —
image-size 图片大小(宽度) number — —
description 文本描述 string — "暂无数据"
  • image 属性:用于设置占位图的路径,支持网络图片地址或本地图片路径。

    <template>
      <el-empty 
        image="https://example.com/image.png" 
        description="找不到您要的内容">
      </el-empty>
    </template>
    
    1
    2
    3
    4
    5
    6
  • image-size 属性:用于自定义图片的宽度,单位为像素。可以通过此属性调整图片的大小以适应不同的布局需求。

    <template>
      <el-empty 
        image="https://example.com/image.png" 
        image-size="200" 
        description="暂无数据">
      </el-empty>
    </template>
    
    1
    2
    3
    4
    5
    6
    7
  • description 属性:用于设置显示的文本描述,默认描述为 "暂无数据"。

    <template>
      <el-empty 
        description="找不到数据">
      </el-empty>
    </template>
    
    1
    2
    3
    4
    5

# 3. Empty 插槽

Empty 组件提供了以下几个插槽,用于自定义内容:

插槽名 说明
default 自定义底部内容
image 自定义图片
description 自定义描述文字
  • default 插槽:用于在组件底部插入自定义内容,例如操作按钮。

    <template>
      <el-empty description="暂无数据">
        <el-button type="primary">刷新</el-button>
      </el-empty>
    </template>
    
    1
    2
    3
    4
    5
  • image 插槽:用于自定义显示的图片,插槽内容将替代 image 属性。

    <template>
      <el-empty description="暂无数据">
        <template #image>
          <img src="https://example.com/custom-image.png" alt="Custom Image">
        </template>
      </el-empty>
    </template>
    
    1
    2
    3
    4
    5
    6
    7
  • description 插槽:用于自定义描述文字,插槽内容将替代 description 属性。

    <template>
      <el-empty>
        <template #description>
          <span>没有找到相关数据,请稍后重试</span>
        </template>
      </el-empty>
    </template>
    
    1
    2
    3
    4
    5
    6
    7

# 4. 常用示例

# 自定义图片和描述

通过设置 image 和 description 属性自定义占位图片和描述文字。

<template>
  <el-empty 
    image="https://example.com/no-data.png" 
    description="暂无内容显示">
  </el-empty>
</template>
1
2
3
4
5
6
  • 自定义图片:图片地址通过 image 属性指定,展示替代图片。
  • image-20240810114400156

# 自定义图片大小

通过 image-size 属性调整图片的大小,使其适应不同的布局需求。

<template>
  <el-empty 
    image="https://example.com/no-data.png" 
    image-size="150" 
    description="没有找到相关内容">
  </el-empty>
</template>
1
2
3
4
5
6
7
  • 图片大小控制:使用 image-size 属性设置图片宽度,使其在页面上显示得更合适。
  • image-20240810114426449

# 插槽自定义内容

通过 default 插槽自定义组件底部的内容,例如增加一个按钮以便用户操作。

<template>
  <el-empty description="没有内容">
    <el-button type="primary" @click="handleRefresh">刷新页面</el-button>
  </el-empty>
</template>

<script>
export default {
  methods: {
    handleRefresh() {
      // 自定义刷新逻辑
      this.$message('页面已刷新');
    }
  }
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • 自定义底部内容:通过 default 插槽添加操作按钮,让组件更加实用。
  • image-20240810114515944

# 完全自定义内容

通过 image 和 description 插槽完全自定义显示的图片和描述内容。

<template>
  <el-empty>
    <template #image>
      <img src="https://example.com/empty-state.png" alt="自定义图片">
    </template>
    <template #description>
      <span>这里什么都没有,请稍后再试。</span>
    </template>
    <el-button type="primary">重试</el-button>
  </el-empty>
</template>
1
2
3
4
5
6
7
8
9
10
11
  • 完全自定义:通过 image 和 description 插槽替换默认的图片和文本描述,提供更灵活的定制能力。
  • image-20240810114556220

总结

  • 灵活的属性配置:通过 image、image-size 和 description 属性,用户可以方便地定制组件的显示内容。
  • 插槽支持:提供多个插槽,允许开发者自由定制显示的图片、描述和底部内容,使组件更加灵活。
  • 适用场景广泛:Empty 组件适用于任何数据为空的场景,通过简单的设置即可提升用户体验。

# 5. 仿B站404页面

image-20240810122057140

<template>
  <div class="not-found">
    <el-card class="card" shadow="hover">
      <div class="sign">
        <p>肥肠抱歉,你要找的页面不见了</p>
      </div>
    </el-card>
    <div class="additional-content">
      <el-button type="primary" @click="goBack">返回上一页</el-button>
      <!-- <el-button type="text" @click="goHome">回到首页</el-button> -->
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    goHome() {
      this.$router.push('/');
    }
  }
};
</script>

<style scoped>
.not-found {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
  padding: 20px;
}

.card {
  width: 600px;
  /* 增加宽度 */
  height: 350px;
  /* 增加高度 */
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 40px;
  /* 增加底部间距 */
  transition: transform 0.3s ease;
  text-align: center;
  background-color: #fff;
  border-radius: 12px;
}

.card:hover .sign {
  transform: scale(1.1);
}

.sign {
  width: 350px;
  /* 增加宽度 */
  height: 100px;
  /* 增加高度 */
  background-color: #e6f7ff;
  color: #409eff;
  font-size: 22px;
  /* 增加字体大小 */
  font-weight: bold;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 2px solid #409eff;
  border-radius: 8px;
  position: relative;
  text-align: center;
  transition: transform 0.3s ease;
}

.sign::before,
.sign::after {
  content: "";
  width: 12px;
  /* 增加宽度 */
  height: 60px;
  /* 增加高度 */
  background-color: #409eff;
  position: absolute;
  top: -60px;
  /* 调整位置 */
}

.sign::before {
  left: 60px;
}

.sign::after {
  right: 60px;
}

.sign p {
  margin: 0;
  transform: rotate(-5deg);
}

.additional-content {
  display: flex;
  gap: 20px;
  /* 增加按钮间距 */
}

.el-button {
  padding: 15px 30px;
  /* 增加按钮尺寸 */
  border-radius: 8px;
  /* 调整按钮圆角 */
  font-size: 16px;
  /* 增加字体大小 */
}

.el-button.text {
  color: #409eff;
}

.el-button.text:hover {
  color: #66b1ff;
}
</style>
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
编辑此页 (opens new window)
上次更新: 2024/12/28, 18:32:08
骨架屏组件 (Skeleton)
描述列表组件 (Descriptions)

← 骨架屏组件 (Skeleton) 描述列表组件 (Descriptions)→

Theme by Vdoing | Copyright © 2019-2025 程序员scholar
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式