VUE中引入第三方断点续传js指向问题 执行不到方法

博客讨论了在Vue CLI3项目中遇到的第三方断点续传库fcup的引用问题。在jQuery环境中正常使用,但在Vue中通过import导入后,对象指向发生错误。解决方案是将fcup方法改为Vue中可用的形式,通过修改库的导出方式,使其能在Vue组件内正确调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决方案见文末

jQuery环境是正常的

如下的js导出
在这里插入图片描述
JQ环境使用:

var up = new fcup({
                        id: "upid", // 绑定id
                        url: "/api/cloudrive/userFile/upchunkFile", // url地址
                        checkurl: "/api/cloudrive/userFile/isUpload", // 检查上传url地址
                        //type: "jpg,png,jpeg,gif", // 限制上传类型,为空不限制
                        shardsize: "8", // 每次分片大小,单位为M,默认1M
                        minsize: '', // 最小文件上传M数,单位为M,默认为无
                        maxsize: "20000", // 上传文件最大M数,单位为M,默认200M
                        // headers: {"version": "fcup-v2.0"}, // 附加的文件头,默认为null, 请注意指定header头时将不能进行跨域操作
                        apped_data: subData, //每次上传的附加数据
                        // 定义错误信息
                        errormsg: {
                            1000: "未找到上传id",
                            1001: "类型不允许上传",
                            1002: "上传文件过小",
                            1003: "上传文件过大",
                            1004: "上传请求超时"
                        },
                        // 错误提示
                        error: function (msg) {
                            alert(msg);
                        },
                        // 初始化事件
                        start: function () {
                            Progress(0);
                        },
                        // 等待上传事件,可以用来loading
                        beforeSend: function () {
                            console.log('等待请求中');
                        },
                        // 上传进度事件
                        progress: function (num, other) {
                            Progress(num);
                            console.log(num);
                            console.log('上传进度' + num);
                            console.log("上传类型" + other.type);
                            console.log("已经上传" + other.current);
                            console.log("剩余上传" + other.surplus);
                            console.log("已用时间" + other.usetime);
                            console.log("预计时间" + other.totaltime);
                        },
                        // 检查地址回调,用于判断文件是否存在,类型,当前上传的片数等操作
                        checksuccess: function (res) {
                            res = res ? eval('(' + res + ')') : '';
                            if (res.code == 200) {
                                var data = res.data;
                                // 秒传存储
                                if (data.status == 1) {
                                    Progress(100);
                                    bootbox.alert('秒传完成!');
                                    modal.hide();
                                    //刷新
                                    DriveFileManager.refresh();
                                    return false;
                                }
                                // 已经上传
                                if (data.status == 2) {
                                    // 起始上传的切片要从1开始
                                    var fileIndex = data.fileIndex ? data.fileIndex : 1;
                                    // 设置上传切片的起始位置
                                    up.setshard(fileIndex);
                                    // 如果接口没有错误,必须要返回true,才不会终止上传
                                    return true;
                                }
                                if (data.status == 4) {
                                    //从0开始上传
                                    return true;
                                }
                            } else {
                                bootbox.alert(res.message);
                                return false;
                            }
                        },
                        // 上传成功回调,回调会根据切片循环,要终止上传循环,必须要return false,成功的情况下要始终返回true;
                        success: function (res) {
                            res = res ? eval('(' + res + ')') : '';
                            if (res.code == 200) {
                                // 如果接口没有错误,必须要返回true,才不会终止上传循环
                                if (res.data.status == 2) {
                                    bootbox.alert("上传完成!");
                                    modal.hide();
                                    //刷新
                                    DriveFileManager.refresh();

                                }
                                return true;
                            } else {
                                bootbox.alert(res.message);
                                return false;
                            }
                        }
                    });

Vue cli3使用import引入

使用时仍然使用JQ的方式时,对象指向的是在这里插入图片描述
SparkMD5,也就是
在这里插入图片描述
所以要在VUE中使用 fcup 方法,我将这个方法,改为了

export const fcup = function(config) 

的形式
改造如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值