【问题标题】:Why does the update of this computed property fail in my Vue 3 app?为什么在我的 Vue 3 应用程序中更新此计算属性失败?
【发布时间】:2023-12-13 12:27:01
【问题描述】:

我正在使用 Vue 3 和 Napster API 开发一个音频播放器

项目详情

我在基于 CSS 关键帧的动画和 isSpinning 计算属性的帮助下制作了乙烯基旋转。

我希望黑胶唱片在到达当前曲目的结尾时停止旋转,这就是isSpinning 有这个“公式”的原因:

isSpinning() {
  return this.isPlaying && !this.player.ended;
}

const musicApp = {
  data() {
    return {
      player: new Audio(),
      trackCount: 0,
      tracks: [],
      muted: false,
      isPlaying: false
    };
  },
  methods: {
    async getTracks() {
      try {
        const response = await axios
          .get(
            "https://api.napster.com/v2.1/tracks/top?apikey=ZTk2YjY4MjMtMDAzYy00MTg4LWE2MjYtZDIzNjJmMmM0YTdm"
          )
          .catch((error) => {
            console.log(error);
          });
        this.tracks = response;
        this.tracks = response.data.tracks;
      } catch (error) {
        console.log(error);
      }
    },
    nextTrack() {
      if (this.trackCount < this.tracks.length - 1) {
        this.trackCount++;
        this.setPlayerSource();
        this.playPause();
      }
    },
    prevTrack() {
      if (this.trackCount >= 1) {
        this.trackCount--;
        this.setPlayerSource();
        this.playPause();
      }
    },
    setPlayerSource() {
      this.player.src = this.tracks[this.trackCount].previewURL;
    },
    playPause() {
      if (this.player.paused) {
        this.isPlaying = true;
        this.player.play();
      } else {
        this.isPlaying = false;
        this.player.pause();
      }
    },
    toggleMute() {
      this.player.muted = !this.player.muted;
      this.muted = this.player.muted;
    }
  },
  async created() {
    await this.getTracks();
    this.setPlayerSource();
  },
  computed: {
    isSpinning() {
      return this.isPlaying && !this.player.ended;
    }
  }
};

Vue.createApp(musicApp).mount("#audioPlayer");
html,
body {
  margin: 0;
  padding: 0;
  font-size: 16px;
}

body * {
  box-sizing: border-box;
  font-family: "Montserrat", sans-serif;
}

@-webkit-keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.player-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #2998ff;
  background-image: linear-gradient(62deg, #2998ff 0%, #5966eb 100%);
}

#audioPlayer {
  width: 300px;
  height: 300px;
  border-radius: 8px;
  position: relative;
  overflow: hidden;
  background-color: #00ca81;
  background-image: linear-gradient(160deg, #00ca81 0%, #ffffff 100%);
  box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.volume {
  color: #ff0057;
  opacity: 0.9;
  display: inline-block;
  width: 20px;
  font-size: 20px;
  position: absolute;
  top: 5px;
  right: 6px;
  cursor: pointer;
}

.album {
  width: 100%;
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.album-items {
  padding: 0 10px;
  text-align: center;
}

.cover {
  width: 150px;
  height: 150px;
  margin: auto;
  box-shadow: 0px 5px 12px 0px rgba(0, 0, 0, 0.17);
  border-radius: 50%;
  background: url("https://w7.pngwing.com/pngs/710/955/png-transparent-vinyl-record-artwork-phonograph-record-compact-disc-lp-record-disc-jockey-symbol-miscellaneous-classical-music-sound.png") center top transparent;
  background-size: cover;
}

.cover.spinning {
  webkit-animation: spin 6s linear infinite;
  /* Safari */
  animation: spin 6s linear infinite;
}

.info {
  width: 100%;
  padding-top: 5px;
  color: #000;
  opacity: 0.85;
}

.info h1 {
  font-size: 11px;
  margin: 5px 0 0 0;
}

.info h2 {
  font-size: 10px;
  margin: 3px 0 0 0;
}

.to-bottom {
  width: 100%;
  margin-top: auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.track {
  background-color: #ff0057;
  opacity: 0.9;
  height: 3px;
  width: 100%;
}

.controls {
  width: 150px;
  display: flex;
  height: 60px;
  justify-content: space-between;
  align-items: center;
}

.controls .navigate {
  display: flex;
  box-shadow: 1px 2px 7px 2px rgba(0, 0, 0, 0.09);
  width: 33px;
  height: 33px;
  line-height: 1;
  color: #ff0057;
  cursor: pointer;
  background: #fff;
  opacity: 0.9;
  border-radius: 50%;
  text-align: center;
  justify-content: center;
  align-items: center;
}

.controls .navigate.disabled {
  pointer-events: none;
  color: #606060;
  background-color: #f7f7f7;
}

.controls .navigate.navigate-play {
  width: 38px;
  height: 38px;
}

.navigate-play .fa-play {
  margin-left: 3px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://unpkg.com/axios@0.22.0/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue@next"></script>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;500&display=swap" rel="stylesheet">

<div class="player-container">
  <div id="audioPlayer">
    <span class="volume" @click="toggleMute">
      <i v-show="!muted" class="fa fa-volume-up"></i>
      <i v-show="muted" class="fa fa-volume-off"></i>
    </span>
    <div class="album">
      <div class="album-items">
        <div class="cover" :class="{'spinning' : isSpinning}"></div>
        <div class="info">
          <h1>{{tracks[trackCount].name}}</h1>
          <h2>{{tracks[trackCount].artistName}}</h2>
        </div>
      </div>
    </div>

    <div class="to-bottom">
      <div class="track"></div>
      <div class="controls">
        <div class="navigate navigate-prev" :class="{'disabled' : trackCount == 0}" @click="prevTrack">
          <i class="fa fa-step-backward"></i>
        </div>
        <div class="navigate navigate-play" @click="playPause">
          <i v-show="!isPlaying" class="fa fa-play"></i>
          <i v-show="isPlaying" class="fa fa-pause"></i>
        </div>
        <div class="navigate navigate-next" :class="{'disabled' : trackCount == tracks.length - 1}" @click="nextTrack">
          <i class="fa fa-step-forward"></i>
        </div>
      </div>
    </div>
  </div>
</div>

问题

但是,令我惊讶的是,该应用并没有意识到 this.player.ended 的值已更改(或应该更改)这一事实。

我做错了什么?

【问题讨论】:

    标签: javascript css vue.js vuejs3


    【解决方案1】:

    Audio 对象的功能与 Vue 中的普通 JavaScript 对象不同,因为当 Audio 对象更改状态时,Vue 用来抽象状态更改观察的内部结构将不会被维护。换句话说,允许 Vue 检测到 Audio 对象从 ended === false 切换到 ended === true 的东西将不起作用,从而阻止 Vue 知道该组件需要更新。

    如果您希望观察ended 状态的变化,那么您需要在created 挂钩中为对象添加自定义事件侦听器以切换旋转状态并简单地删除ended 检查:

    const musicApp = {
      data() {
        return {
          player: new Audio(),
          trackCount: 0,
          tracks: [],
          muted: false,
          isPlaying: false
        };
      },
      methods: {
        async getTracks() {
          try {
            const response = await axios
              .get(
                "https://api.napster.com/v2.1/tracks/top?apikey=ZTk2YjY4MjMtMDAzYy00MTg4LWE2MjYtZDIzNjJmMmM0YTdm"
              )
              .catch((error) => {
                console.log(error);
              });
            this.tracks = response;
            this.tracks = response.data.tracks;
          } catch (error) {
            console.log(error);
          }
        },
        nextTrack() {
          if (this.trackCount < this.tracks.length - 1) {
            this.trackCount++;
            this.setPlayerSource();
            this.playPause();
          }
        },
        prevTrack() {
          if (this.trackCount >= 1) {
            this.trackCount--;
            this.setPlayerSource();
            this.playPause();
          }
        },
        setPlayerSource() {
          this.player.src = this.tracks[this.trackCount].previewURL;
        },
        playPause() {
          if (this.player.paused) {
            this.isPlaying = true;
            this.player.play();
          } else {
            this.isPlaying = false;
            this.player.pause();
          }
        },
        toggleMute() {
          this.player.muted = !this.player.muted;
          this.muted = this.player.muted;
        }
      },
      async created() {
        await this.getTracks();
        this.setPlayerSource();
        this.player.addEventListener('ended', () => {
          this.isPlaying = false;
        });
      },
      computed: {
        isSpinning() {
          return this.isPlaying;
        }
      }
    };
    
    Vue.createApp(musicApp).mount("#audioPlayer");
    html,
    body {
      margin: 0;
      padding: 0;
      font-size: 16px;
    }
    
    body * {
      box-sizing: border-box;
      font-family: "Montserrat", sans-serif;
    }
    
    @-webkit-keyframes spin {
      0% {
        -webkit-transform: rotate(0deg);
      }
      100% {
        -webkit-transform: rotate(360deg);
      }
    }
    
    @keyframes spin {
      0% {
        transform: rotate(0deg);
      }
      100% {
        transform: rotate(360deg);
      }
    }
    
    .player-container {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      background-color: #2998ff;
      background-image: linear-gradient(62deg, #2998ff 0%, #5966eb 100%);
    }
    
    #audioPlayer {
      width: 300px;
      height: 300px;
      border-radius: 8px;
      position: relative;
      overflow: hidden;
      background-color: #00ca81;
      background-image: linear-gradient(160deg, #00ca81 0%, #ffffff 100%);
      box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    
    .volume {
      color: #ff0057;
      opacity: 0.9;
      display: inline-block;
      width: 20px;
      font-size: 20px;
      position: absolute;
      top: 5px;
      right: 6px;
      cursor: pointer;
    }
    
    .album {
      width: 100%;
      flex: 1;
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      align-items: center;
    }
    
    .album-items {
      padding: 0 10px;
      text-align: center;
    }
    
    .cover {
      width: 150px;
      height: 150px;
      margin: auto;
      box-shadow: 0px 5px 12px 0px rgba(0, 0, 0, 0.17);
      border-radius: 50%;
      background: url("https://w7.pngwing.com/pngs/710/955/png-transparent-vinyl-record-artwork-phonograph-record-compact-disc-lp-record-disc-jockey-symbol-miscellaneous-classical-music-sound.png") center top transparent;
      background-size: cover;
    }
    
    .cover.spinning {
      webkit-animation: spin 6s linear infinite;
      /* Safari */
      animation: spin 6s linear infinite;
    }
    
    .info {
      width: 100%;
      padding-top: 5px;
      color: #000;
      opacity: 0.85;
    }
    
    .info h1 {
      font-size: 11px;
      margin: 5px 0 0 0;
    }
    
    .info h2 {
      font-size: 10px;
      margin: 3px 0 0 0;
    }
    
    .to-bottom {
      width: 100%;
      margin-top: auto;
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
    }
    
    .track {
      background-color: #ff0057;
      opacity: 0.9;
      height: 3px;
      width: 100%;
    }
    
    .controls {
      width: 150px;
      display: flex;
      height: 60px;
      justify-content: space-between;
      align-items: center;
    }
    
    .controls .navigate {
      display: flex;
      box-shadow: 1px 2px 7px 2px rgba(0, 0, 0, 0.09);
      width: 33px;
      height: 33px;
      line-height: 1;
      color: #ff0057;
      cursor: pointer;
      background: #fff;
      opacity: 0.9;
      border-radius: 50%;
      text-align: center;
      justify-content: center;
      align-items: center;
    }
    
    .controls .navigate.disabled {
      pointer-events: none;
      color: #606060;
      background-color: #f7f7f7;
    }
    
    .controls .navigate.navigate-play {
      width: 38px;
      height: 38px;
    }
    
    .navigate-play .fa-play {
      margin-left: 3px;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
    <script src="https://unpkg.com/axios@0.22.0/dist/axios.min.js"></script>
    <script src="https://unpkg.com/vue@next"></script>
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;500&display=swap" rel="stylesheet">
    
    <div class="player-container">
      <div id="audioPlayer">
        <span class="volume" @click="toggleMute">
          <i v-show="!muted" class="fa fa-volume-up"></i>
          <i v-show="muted" class="fa fa-volume-off"></i>
        </span>
        <div class="album">
          <div class="album-items">
            <div class="cover" :class="{'spinning' : isSpinning}"></div>
            <div class="info">
              <h1>{{tracks[trackCount].name}}</h1>
              <h2>{{tracks[trackCount].artistName}}</h2>
            </div>
          </div>
        </div>
    
        <div class="to-bottom">
          <div class="track"></div>
          <div class="controls">
            <div class="navigate navigate-prev" :class="{'disabled' : trackCount == 0}" @click="prevTrack">
              <i class="fa fa-step-backward"></i>
            </div>
            <div class="navigate navigate-play" @click="playPause">
              <i v-show="!isPlaying" class="fa fa-play"></i>
              <i v-show="isPlaying" class="fa fa-pause"></i>
            </div>
            <div class="navigate navigate-next" :class="{'disabled' : trackCount == tracks.length - 1}" @click="nextTrack">
              <i class="fa fa-step-forward"></i>
            </div>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

      【解决方案2】:

      尝试添加ended 事件

      https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement#events

      const audioElement = new Audio('car_horn.wav');
      audioElement.addEventListener('ended', () => {
        this.isPlaying = false
      })
      

      【讨论】:

        最近更新 更多