vue+elementUI项目中拼接url字符串

在elementUI 中难免会使用table组件完成显示表格数据的功能,如下例子:将后台返回的数据的一个参数再次传给后台接口链接打印excel表格。

引用段落,首先使用scope.row的方式获取后台返回的参数time,然后通过在el-button上的点击事件触发函数getTimeDetail(),局部代码如下:

<el-table-column label="打印数据" width="150" align="center" fixed="right">
                <template slot-scope="scope">
                    <el-button type="primary" size="small" icon="el-icon-download" style="margin-top: 0" @click="getTimeDetail(scope.row.time)">导出数据明细</el-button>
                </template>
</el-table-column>
export default {
        name: "bill",
        data() {
            return {
                time:'',   //2019-04-02
                exportBaseUrl: 接口url固定头部
            }
        },
methods: {
            //导出明细
            getTimeDetail(time){
                this.time = time;
                let obj = this.getUserList(true);
                let tmpa = document.createElement("a");
                tmpa.href = obj; //绑定a标签
                tmpa.click(); //模拟点击实现下载
                setTimeout(function () { //延时释放
                    URL.revokeObjectURL(obj); //用URL.revokeObjectURL()来释放这个object URL
                }, 100);
            },
           // 导出明细
            getUserList(is_exoprt = false) {
                let params = { };   //params是对象类型
                if (is_exoprt) {
                    let token = getMallToken();
                    let RegExp = /^(\d{4})(-)(\d{2})(-)(\d{2})$/;      //判断日期格式
                    if(RegExp.test(this.time)){
                        let substring = this.time.substring(0)  //2019-04-02
                        params.time = substring;
                        var exportUrl = this.exportBaseUrl + '后台提供接口?token=' + token;

                    }else{
                        let substring = this.time.substring(0,7)  //2019
                        params.time = substring;
                        var exportUrl = this.exportBaseUrl + '后台提供接口?token=' + token;
                    }
                    return exportUrl + urlEncode(params);
                } else {
                    this.loading = true;
                    distributorList(params).then(res => {
                        this.loading = false;
                    })
                }
            },
            urlEncode(param, key, encode) {
                //param表示传的数据,key表示键值对的值,encode表示URI组件编码
                 if (param == null) return '';
                 let paramStr = '';
                 let t = typeof (param);    //判断param的类型
                 if (t == 'string' || t == 'number' || t == 'boolean') {     //若为简单类型,直接拼接
                     paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param);
                 } else {     //若为复杂类型,递归解析
                     for (let i in param) {
                     let k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
                     paramStr += urlEncode(param[i], k, encode);    //多次调用解析
                }
            }
          return paramStr;
         }
   }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字阅读 5,451评论 0 9
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,082评论 0 29
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • element-ui 文档 Vue项目接口文档地址 博客 session 和 cookie等 学什么? 1 如何使...
    cj_jax阅读 3,975评论 0 10
  • 很多时候,大多数人,特别害怕别人讨论自己的缺点,觉得自己不足。当然我也一样,我讨厌别人说你好矮,或者说你脸...
    林空鹿饮溪tea阅读 390评论 1 2