vue element ui axios 基础版本demo

55 篇文章 0 订阅
37 篇文章 1 订阅
<html>
<head>
    <meta charset="UTF-8">
    <!--http://localhost:6975/test/esl.html-->
    <!-- 引入样式 -->
     <!-- 引入组件vant-->
    <!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>-->
    <!--<script src="https://cdn.jsdelivr.net/npm/vant@2.1/lib/vant.min.js"></script>-->
<!--element-->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://cdn.bootcss.com/vue/2.6.11/vue.min.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <!-- <script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js"></script>
      <script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script>
      <script src="https://cdn.bootcss.com/axios/0.19.2/axios.min.js"></script> -->
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style>
        el-table-column{
            width: 10px;
        }
        .table-expand {
            font-size: 0;
        }
        .table-expand label {
            width: 100px;
            color: #99a9bf;
        }
        .table-expand .el-form-item {
            margin-right: 0;
            margin-bottom: 0;
        }
        .stateOn {
            border: rgb(245, 8, 8);
        }
    </style>
</head>

<body>
<div id="app">

    <!--<br>-->
    <!--<span v-bind:title="title">&lt;!&ndash;"v-bind:"为属性指令,为vue所特有指令&ndash;&gt;-->
 {{message}}
    <!--</span>-->
    <div>正常呼叫</div>
    <el-button type="primary" @click="callAnother">刷新</el-button>
    <template>
    <el-table  :data="contents" style="width: 100%" stripe>
        <el-table-column align="center" label="事件名字" prop="Event-Name"  width="180"></el-table-column>
        <el-table-column align="center" label="Channel-State" prop="Channel-State" width="180"></el-table-column>
        <el-table-column align="center" width="180" label="Channel-Call-State" prop="Channel-Call-State" ></el-table-column>
        <el-table-column align="center" width="180" label="Answer-State" prop="Answer-State" ></el-table-column>
        <el-table-column align="center"  width="180" label="Call-Direction" prop="Call-Direction" ></el-table-column>
        <el-table-column align="center" width="180"  label="variable_current_application_data" prop="variable_current_application_data" ></el-table-column>
        <el-table-column align="center" width="180"  label="呼叫id" prop="Unique-ID" ></el-table-column>
        <el-table-column align="center" width="180"  label="from" prop="from" ></el-table-column>
    </el-table>
    </template>
</div>
</body>
<script>
    var str=[];
    var list=[];
    str[0]='hello word';
    str[1]='You loaded this page on';
    var app = new Vue({
        el: '#app',
        data: {
            message:'1111',
            title:'pageon' + new Date(),
            contents:[]
            /*通过el相应的id获取容器,通过data逐项为相应的参数赋值,所赋值可为变量(可通过ajax获取值,进行页面的渲染)*/
        },
        methods:{
            callAnother(){
                var that=this
                console.log("-----刷新");
                getMore ("http://localhost:6975/fs/eventsave").then(res => {
                    // dataSource = res.data//将请求得到的参数赋值给全局变量dataSource
                    // console.log(res);
                    if(res.data.data.list&&res.data.data.list.length>0){
                    that.contents=res.data.data.list
                    this.message="55555"
                    console.log(" contents=")
                        console.log(that.contents)
                    }
                } ).catch(error => {
                    console.log(error)
                })
            }
        }
    })
    function getMore (url, data) {
        return axios({
            // methods: 'post',//若无此项,默认get
            url: url,
            data: data//get-params
        })
    }
</script>
</html>
断点续传是指在文件上传过程中,如果因为网络或其他原因导致上传中断,可以在中断的位置继续上传,而不需要重新上传整个文件。下面是使用 Vue + Element + Axios + qs 实现断点续传的步骤: 1. 安装 ElementAxios ``` npm install element-ui axios --save ``` 2. 创建上传组件 在 Vue 的组件中,使用 Element 的上传组件和 Axios 进行文件上传。首先,我们需要在模板中添加一个上传组件: ```html <el-upload class="upload-demo" ref="upload" :action="uploadUrl" :data="uploadData" :file-list="fileList" :auto-upload="false" :on-success="handleSuccess" :on-error="handleError" > <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <el-button size="small" type="success" @click="submitUpload">上传到服务器</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> ``` 其中,`uploadUrl` 是上传接口的地址,`uploadData` 是上传接口的参数,`fileList` 是上传的文件列表,`handleSuccess` 和 `handleError` 是上传成功和失败的回调函数。 3. 实现上传方法 在 Vue 的方法中,实现文件上传的方法: ```javascript submitUpload() { this.$refs.upload.submit(); }, handleSuccess(response, file, fileList) { // 处理上传成功的逻辑 }, handleError(err, file, fileList) { // 处理上传失败的逻辑 } ``` 在 `submitUpload` 方法中,调用上传组件的 `submit` 方法进行文件上传。在 `handleSuccess` 方法中,处理上传成功的逻辑,包括显示上传成功的提示信息、更新文件列表等。在 `handleError` 方法中,处理上传失败的逻辑,包括显示上传失败的提示信息、更新文件列表等。 4. 实现断点续传 要实现断点续传,需要在上传组件中添加 `before-upload` 和 `on-progress` 事件,分别处理上传前和上传中的逻辑。在 `before-upload` 事件中,检查文件是否已经上传过,如果上传过,就设置上传的起点为上次上传结束的位置,否则上传整个文件。在 `on-progress` 事件中,更新上传进度。 ```html <el-upload class="upload-demo" ref="upload" :action="uploadUrl" :data="uploadData" :file-list="fileList" :auto-upload="false" :before-upload="beforeUpload" :on-progress="onProgress" :on-success="handleSuccess" :on-error="handleError" > ``` ```javascript beforeUpload(file) { // 判断文件是否已经上传过 if (localStorage.getItem(file.name)) { // 设置上传的起点为上次上传结束的位置 this.uploadData.start = JSON.parse(localStorage.getItem(file.name)).end; } else { // 上传整个文件 this.uploadData.start = 0; } }, onProgress(event, file, fileList) { // 更新上传进度 const progress = Math.round((event.loaded / event.total) * 100); this.$set(file, "progress", progress); } ``` 在 `before-upload` 事件中,使用 `localStorage` 存储文件上传结束位置,以便下次继续上传。在 `on-progress` 事件中,计算上传进度并更新文件列表中对应文件的进度。 5. 实现暂停上传和恢复上传 要实现暂停上传和恢复上传,可以在上传组件中添加两个按钮,分别用于暂停和恢复上传。在暂停上传时,保存上传进度并中止上传。在恢复上传时,从上次保存的上传进度开始上传。 ```html <el-upload ... > <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <el-button size="small" type="success" @click="submitUpload">上传到服务器</el-button> <el-button size="small" type="warning" v-show="!isUploading" @click="resumeUpload">恢复上传</el-button> <el-button size="small" type="danger" v-show="isUploading" @click="pauseUpload">暂停上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> ``` ```javascript data() { return { isUploading: false, uploadProgress: 0 }; }, methods: { submitUpload() { this.$refs.upload.submit(); this.isUploading = true; }, pauseUpload() { this.$refs.upload.abort(); this.isUploading = false; }, resumeUpload() { this.$refs.upload.submit(); this.isUploading = true; }, beforeUpload(file) { ... }, onProgress(event, file, fileList) { ... this.uploadProgress = progress; file.progress = progress; } } ``` 在 Vue 的数据中,添加 `isUploading` 和 `uploadProgress` 两个变量,分别表示上传状态和上传进度。在方法中,实现暂停上传和恢复上传的逻辑,使用 `isUploading` 变量控制按钮的显示。在 `before-upload` 和 `on-progress` 事件中,更新上传状态和上传进度。 6. 完整代码 ```html <template> <div> <el-upload class="upload-demo" ref="upload" :action="uploadUrl" :data="uploadData" :file-list="fileList" :auto-upload="false" :before-upload="beforeUpload" :on-progress="onProgress" :on-success="handleSuccess" :on-error="handleError" > <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <el-button size="small" type="success" @click="submitUpload">上传到服务器</el-button> <el-button size="small" type="warning" v-show="!isUploading" @click="resumeUpload">恢复上传</el-button> <el-button size="small" type="danger" v-show="isUploading" @click="pauseUpload">暂停上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </div> </template> <script> import { Upload, Button } from "element-ui"; import axios from "axios"; import qs from "qs"; export default { name: "UploadComponent", components: { "el-upload": Upload, "el-button": Button }, data() { return { isUploading: false, uploadProgress: 0, uploadUrl: "/upload", uploadData: { start: 0 }, fileList: [] }; }, methods: { submitUpload() { this.$refs.upload.submit(); this.isUploading = true; }, pauseUpload() { this.$refs.upload.abort(); this.isUploading = false; }, resumeUpload() { this.$refs.upload.submit(); this.isUploading = true; }, beforeUpload(file) { if (localStorage.getItem(file.name)) { this.uploadData.start = JSON.parse(localStorage.getItem(file.name)).end; } else { this.uploadData.start = 0; } }, onProgress(event, file, fileList) { const progress = Math.round((event.loaded / event.total) * 100); this.uploadProgress = progress; file.progress = progress; localStorage.setItem( file.name, JSON.stringify({ end: this.uploadData.start + event.loaded }) ); }, handleSuccess(response, file, fileList) { this.fileList = fileList; this.isUploading = false; localStorage.removeItem(file.name); this.$message({ message: "上传成功", type: "success" }); }, handleError(err, file, fileList) { this.fileList = fileList; this.isUploading = false; this.$message.error("上传失败"); } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值