【问题标题】:Keep updating progressbar when view is destroyed视图被销毁时继续更新进度条
【发布时间】:2017-04-18 18:22:08
【问题描述】:

在我的 Ionic 2 应用程序中,我使用此功能下载视频文件并跟踪其下载进度:

download()
{
    this.progressbar=true;
    this.downloadbutton=false;
    this.fileTransfer.download('https://...videoURL....mp4', this.file.dataDirectory + 'path/to/downloads/test.mp4').then((entry) => {
        console.log('download complete: ' + entry.toURL());
        this.progressbar=false;
        this.startbutton=true;
    }, (error) => {
        console.log('error');
        console.log(error);
    });

    this.fileTransfer.onProgress(progressEvent => {
        if (progressEvent.lengthComputable) {
            console.log("### Download percentage ###: "+Math.round((progressEvent.loaded / progressEvent.total)*100));
            this.setpercentage((progressEvent.loaded / progressEvent.total) * 100);
        } else {
        }
    });

}

使用setpercentage,我更新页面上的进度条

setpercentage(perc) {
        this.loadProgress = Math.round(perc);
        this.ref.detectChanges();
    }

这一切都很好。当用户离开(返回)时,视图被销毁。视频继续下载(应该如此),但是当我导航回页面时(视频仍在下载时),进度条没有更新:值保持在 0%。

即使视图被破坏,我怎样才能保持进度条更新?

基本上,我想让这个page 具有与tab 相同的功能。当用户在不同的选项卡之间导航时,前一个选项卡的所有内容都会被记住。

非常感谢任何帮助!

更新:

以下是应用程序如何工作的更多信息:

当用户第一次导航到该页面时,会有一个显示“下载电影”的按钮。当用户点击它时,download() 被调用。 this.progressbar=true; 使进度条出现,而this.downloadbutton=false 使下载按钮消失。下载完成后,进度条消失 (this.progressbar=false;) 并出现“播放电影”按钮 (this.startbutton=true;)。 问题是当用户单击下载按钮,然后导航离开并返回此页面时,似乎一切都已重置(尽管电影确实下载了),并且下载按钮是再次显示。

【问题讨论】:

    标签: angular ionic-framework ionic2


    【解决方案1】:

    当视图被销毁时,它要么出错,要么完成你的 observable

     this.fileTransfer.download('https://...videoURL....mp4', 
         this.file.dataDirectory + 'path/to/downloads/test.mp4').then((entry) => {
            console.log('download complete: ' + entry.toURL());
            this.progressbar=false;
            this.startbutton=true;
        }, (error) => {
            this.progressbar=false; //////////////////////////////////
            console.log('error');
            console.log(error);
        });
    

    所有可能的操作都应该改变进度条的状态。

    另外,当视图被销毁时

    实现ngOnDestroy()生命周期钩子来处理相同的情况

    ngOnDestroy(){
          this.progressbar=false; ////////
    }
    

    更新 1:

    下载方法的语法如下

    fileTransfer.download(
        sourceURL,
        targetURL,
        successcallback,
        errorCallBack,
        trustAllHosts,
        options)
    

    source,target,successcallback,errorCallBack 是强制参数

    Docs

    根据语法更新方法。

    【讨论】:

    • 感谢您的回复。当我尝试使用您建议的调整构建项目时,出现构建错误:Supplied parameters do not match any signature of call target.
    • 我没有更改该方法中的任何内容。那你怎么会得到这个错误呢?
    • 我用完整的构建错误更新了我的问题。这个错误太长了,无法回复。
    • 我正在使用 Ionic Native 这个插件。实现略有不同:ionicframework.com/docs/native/transfer
    • 我很抱歉语法错误,更新后的答案会起作用
    猜你喜欢
    • 2011-06-14
    • 2018-04-12
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多