简体   繁体   English

使用 CryptoJS 的 VueJS 哈希文件

[英]VueJS hash files with CryptoJS

I'm reaching out to you because I'm terribly stuck with a Vue project...我联系你是因为我被 Vue 项目困住了......

I'm experiencing a little issue when trying to move a jQuery project over to Vue.我在尝试将 jQuery 项目转移到 Vue 时遇到了一个小问题。 CryptoJS is working a charm and I can create hashes from strings. CryptoJS 很有魅力,我可以从字符串创建哈希。

However, I'm still struggling reading the actual file, as the nested functions are throwing errors.但是,我仍在努力阅读实际文件,因为嵌套函数会引发错误。 Specifically getting errors on the callbackRead function.特别是在callbackRead函数上出错。

App.vue?234e:280 Uncaught TypeError: this.callbackRead is not a function
at FileReader.reader.onload (App.vue?234e:280)

Can you please give me some guidance on how to successfully translate the script to VUE JS?你能给我一些关于如何成功地将脚本翻译成 VUE JS 的指导吗? ( https://medium.com/@0xVaccaro/hashing-big-file-with-filereader-js-e0a5c898fc98 ) ( https://medium.com/@0xVaccaro/hashing-big-file-with-filereader-js-e0a5c898fc98 )

Thanks a lot in advance!!!非常感谢提前!!!

Here is what I got so far: https://codesandbox.io/s/vuejs-file-crypter-kjirp这是我到目前为止得到的: https : //codesandbox.io/s/vuejs-file-crypter-kjirp

Best regards, Mac最好的问候,麦克

The error is coming from this section:错误来自本节:

reader.onload = function(evt) {
    this.callbackRead(this, file, evt, callbackProgress, callbackFinal);
};

The problem is that this refers to the wrong object.问题是this指向错误的对象。 Your onload handler is a different function from the surrounding code and whenever you enter a new function the value of this changes.你的onload处理程序是从周围的代码不同的功能,当你进入一个新的函数值this变化。

There are several possible solutions.有几种可能的解决方案。

Aliasing this :别名this

const that = this;

reader.onload = function(evt) {
    that.callbackRead(that, file, evt, callbackProgress, callbackFinal);
};

Binding this :绑定this

reader.onload = function(evt) {
    this.callbackRead(this, file, evt, callbackProgress, callbackFinal);
}.bind(this);

Using an arrow function, which won't alter the value of this :使用箭头函数,它不会改变this的值:

reader.onload = evt => {
    this.callbackRead(this, file, evt, callbackProgress, callbackFinal);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM