简体   繁体   English

Javascript:无法从回调设置成员变量

[英]Javascript: Cannot set member variable from callback

I'm creating a very simple HTMLElement class for a Javascript widget.我正在为 Javascript 小部件创建一个非常简单的 HTMLElement class。 There is a button which can be used to toggle the microphone recording.有一个按钮可用于切换麦克风录音。 I can log the resulting files to console, and everything works well.我可以将生成的文件记录到控制台,一切正常。 When I try to save this file in the stop() callback and assign it to a member variable I can't access it anymore.当我尝试将此文件保存在stop()回调中并将其分配给成员变量时,我无法再访问它了。

I thought that I could use this in arrow function callbacks like this as of ES6?我认为我可以在 ES6 this的箭头 function 回调中使用它? What am I missing here?我在这里想念什么?

const MicRecorder = require("mic-recorder-to-mp3");

class Microphone extends HTMLElement {
  constructor() {
    super();

    const shadowDOM = this.attachShadow({ mode: "open" });
    this.shadowDOM = shadowDOM;
    this.shadowDOM.appendChild(template.content.cloneNode(true));
    this.toggleRecord = this.toggleRecord.bind(this);

    // Set up mic recorder
    this.recorder = new MicRecorder({
      bitRate: 128,
    });

    this.recording = false;
  }

  connectedCallback() {
    const recordButton = this.shadowRoot.querySelector("#record-button");
    recordButton.onclick = this.toggleRecord;
  }

  submit() {
   console.log(this.file);
   // Unable to access the file.
  }

  toggleRecord() {
    const recordButton = this.shadowRoot.querySelector("#record-button");

    if (recordButton.classList.contains("recording")) {
      this.recorder
        .stop()
        .getMp3()
        .then(([buffer, blob]) => {

          const file = new File(buffer, "myfile.mp3", {
            type: blob.type,
            lastModified: Date.now(),
          });
          this.file = file; // Can I set a member variable like this?
          recordButton.innerHTML = "Re-Record";
          this.recording = false;
          recordButton.classList.remove("recording");
        })
        .catch((e) => {
          alert("We could not record your message.");
        });
    } else {
      this.recorder
        .start()
        .then(() => {
          this.recording = true;
          recordButton.classList.add("recording");
        })
        .catch((e) => {
          alert("Could not start recording.");
        });
    }
  }
}

export default Microphone;

Try saving a reference to this to be used inside your then function尝试保存this的引用以在您的then中使用

...    
if (recordButton.classList.contains("recording")) {
          var self = this;
          self.recorder
            .stop()
            .getMp3()
            .then(([buffer, blob]) => {

              const file = new File(buffer, "myfile.mp3", {
                type: blob.type,
                lastModified: Date.now(),
              });
              self.file = file;
...

The problem was because I didn't properly bind all class functions.问题是因为我没有正确绑定所有class 函数。

constructor() {
    super();

    const shadowDOM = this.attachShadow({ mode: "open" });
    this.shadowDOM = shadowDOM;
    this.shadowDOM.appendChild(template.content.cloneNode(true));

    this.toggleRecord = this.toggleRecord.bind(this);
    this.submit = this.submit.bind(this); // Needed this line!

    // ...
  }

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

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