zoukankan      html  css  js  c++  java
  • [vue] beforeDestroy中dom为null?

    场景:页面支持播放语音 跳转到其他页面时 需要在 beforeDestroy中暂停播放 防止部分型号手机再下个页面中继续播放

    先来看一下代码:

    beforeDestroy() {
          let _audio = document.getElementById('audioIntroduce');
          console.log(_audio);
          _audio.pause();
          this.isPlaying = false;
    }

    结果如下:

    通过 document.getElementById('audioIntroduce') 拿到的dom竟然为null?这不科学

    接着我在 beforeDestroy() 里打印 document 发现打印出来的document对象竟然是我跳转之后的页面对象QAQ

    打断点发现 一旦开始执行 beforeDestroy() 这个方法 页面就跳转到了下一个页面 此时document对象当然就是下一个页面

    尝试打印this对象 发现打印出来的this是当前页面的vue对象,那么既然可以拿到当前页面的this对象 同样的 可以通过ref拿到我们想要的audio元素

    修改后:

    beforeDestroy() {
          let _audio = this.$refs.audioIntroduce;
          console.log(_audio);
          _audio.pause();
          this.isPlaying = false;
    }

    此时拿到的audio对象:

     yeah!done!!

  • 相关阅读:
    [学习笔记]多维偏序
    SCOI2009 游戏
    置换群和Burnside引理,Polya定理
    AC自动机——多个kmp匹配
    51nod 1667 概率好题
    分块——优化的暴力
    [Cqoi2014]数三角形——组合数
    C++ 中的导致编译错误汇总
    哈夫曼树Huffman
    导出查询结果到csv文件
  • 原文地址:https://www.cnblogs.com/yan89/p/11514357.html
Copyright © 2011-2022 走看看