在vue项目新增loading

ElementUI加载动画实践
本文详细介绍了如何在ElementUI项目中实现自定义加载动画,包括使用transition标签、加载条样式设计、通过Vuex管理和$EventBus事件总线更新加载状态的具体步骤。

有时候在elementui项目中一个Loading并不满足需求。所以要额外增加其他的Loading来展示

1 在App.vue中是增加一个transition标签用来过渡

<transition name="load">
    <div v-if="loadShow" class="load">
        <img src="/static/images/load.png"/>
    </div>
</transition>

name是自己命名的class的名称,用来写动画样式,如果不写name  则默认是v

对应样式名称如下:写样式的时候,v的地方要改fade.

如:

.load-enter-active, .load-leave-active {

  transition: opacity .5s;

}

2 加载条样式

.load{
  	position: fixed;
  	z-index: 9000;
  	top: 0;
  	left: 0;
  	right: 0;
  	bottom: 0;
  	width: 100%;
  	height: 100%;
  	background: rgba(0,0,0,0.5);
  }
  .load img{
  	width: 50px;
  	height: 50px;
  	left: 50%;
  	top: 50%;
  	position: absolute;
  	margin-top: -50px;
  	margin-left: -50px;
  	animation: circle 1.5s infinite;
  }
  @keyframes circle{
  	from{
  		transform: rotate(0);
  	}
  	to{
  		transform: rotate(360deg);
  	}
  }
  .load-enter-active, .load-leave-active{
  	transition: opacity .5;
  }

3 状态设置

使用vuex来对loadShow状态进行管理, 更改loadShow的值在App.vue无法检测到状态的变更,需要setInterval()不断获取vuex中loadShow的状态。

所以这里使用$EventBus 事件总线 在main.js中初始化

Vue.prototype.$EventBus = new Vue();

在使用到的地方发送事件

this.$EventBus.$emit('loadShow',true)

在App.vue中接收事件

this.$EventBus.$on('loadShow', (flag)=>{
    this.loadShow= flag
})

### 集成 DeepSeek 问答功能于 Vue 项目 #### 安装依赖项 为了在 Vue 项目中集成 DeepSeek 的问答功能,首先需要安装必要的依赖项。假设已经创建了一个 Vue 项目,则可以通过 npm 或 yarn 来添加所需的软件包。 ```bash npm install @deepseek/api-client axios vue-router ``` 这一步骤确保了应用程序能够调用 DeepSeek 提供的服务接口[^1]。 #### 初始化配置文件 接着,在项目的 `src` 文件夹下新建一个名为 `config.js` 的 JavaScript 文件用于保存 API 密钥和其他全局设置: ```javascript // src/config.js export default { deepSeekApiKey: 'YOUR_DEEPSEEK_API_KEY', baseUrl: 'https://api.deepseek.com/v1' } ``` 这里的 URL 和密钥需替换为实际使用的地址和凭证[^2]。 #### 创建服务层 随后建立一个新的目录结构来容纳与 DeepSeek 相关的功能逻辑。可以在 `src/services` 下新增 `deepSeekService.js` 文件实现数据获取和服务请求处理: ```javascript import axios from 'axios'; import config from '../config'; class DeepSeekService { constructor() { this.apiClient = axios.create({ baseURL: config.baseUrl, headers: { Authorization: `Bearer ${config.deepSeekApiKey}` } }); } async getAnswer(question) { try { const response = await this.apiClient.post('/qa', { question }); return response.data; } catch (error) { console.error('Error fetching answer:', error); throw new Error('Failed to fetch the answer'); } } } const serviceInstance = new DeepSeekService(); export default serviceInstance; ``` 这段代码定义了一个简单的 HTTP 客户端实例化对象,并封装了一个异步方法用来发送 POST 请求给 DeepSeek QA 接口以接收答案[^3]。 #### 组件开发 最后,在适当位置编写 Vue 单文件组件(SFC),比如命名为 `QnAComponent.vue` ,它负责展示输入框让用户提问以及显示返回的结果: ```vue <template> <div class="qna-container"> <input v-model="question" placeholder="Ask me anything..." /> <button @click="fetchAnswer">Submit</button> <p v-if="loading">Loading...</p> <pre>{{ answer }}</pre> </div> </template> <script> import deepSeekService from '@/services/deepSeekService'; export default { data() { return { question: '', loading: false, answer: null }; }, methods: { async fetchAnswer() { this.loading = true; try { this.answer = await deepSeekService.getAnswer(this.question); } finally { this.loading = false; } } } }; </script> <style scoped> .qna-container { max-width: 600px; margin: auto; padding: 2rem; border: 1px solid #ccc; text-align: center; } </style> ``` 上述模板展示了如何构建交互界面并通过事件绑定触发后台查询操作;样式部分则简单设置了容器布局以便更好地呈现效果.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值