用vue+element实现一个下载,卸载游戏的功能。要求:1.下载游戏需要有进度条,2.卸载游戏需要有进度条。并给出代码示例
时间: 2024-03-21 19:42:13 浏览: 49
好的,以下是一个简单的实现示例:
HTML代码:
```html
<template>
<div>
<el-button @click="downloadGame">下载游戏</el-button>
<el-progress :percentage="downloadProgress" v-if="downloading"></el-progress>
<el-button @click="uninstallGame">卸载游戏</el-button>
<el-progress :percentage="uninstallProgress" v-if="uninstalling"></el-progress>
</div>
</template>
```
JS代码:
```javascript
<script>
import axios from 'axios';
export default {
data() {
return {
downloading: false,
downloadProgress: 0,
uninstalling: false,
uninstallProgress: 0
}
},
methods: {
downloadGame() {
this.downloading = true;
axios.get('/downloadGame', {
onDownloadProgress: progressEvent => {
this.downloadProgress = Math.round(progressEvent.loaded / progressEvent.total * 100);
}
})
.then(() => {
this.downloading = false;
this.downloadProgress = 0;
this.$message.success('下载完成!');
})
.catch(() => {
this.downloading = false;
this.downloadProgress = 0;
this.$message.error('下载失败!');
});
},
uninstallGame() {
this.uninstalling = true;
axios.get('/uninstallGame', {
onDownloadProgress: progressEvent => {
this.uninstallProgress = Math.round(progressEvent.loaded / progressEvent.total * 100);
}
})
.then(() => {
this.uninstalling = false;
this.uninstallProgress = 0;
this.$message.success('卸载完成!');
})
.catch(() => {
this.uninstalling = false;
this.uninstallProgress = 0;
this.$message.error('卸载失败!');
});
}
}
}
</script>
```
这段代码中,我们使用了axios来向后端发送请求,并使用onDownloadProgress回调函数来实时更新进度条的值。同时,我们使用了Element UI中的Progress组件来显示进度条,并使用v-if指令来控制进度条的显示与隐藏。最后,我们使用Element UI中的Message组件来提示用户下载/卸载完成或失败的信息。
阅读全文