是什么原因导致此 Vue 3 应用程序中创建的钩子执行期间出错?

What causes the error during execution of created hook in this Vue 3 application?

我正在使用 Vue 3 和 Napster API。

开发 音频播放器

项目详情

有一个 下一个 和一个 上一个 按钮可以帮助导航曲目。

我有一个 player 对象 Audio() 构造函数。我使用 setPlayerSource() 方法设置当前曲目的源。

const musicApp = {
  data() {
    return {
      player: new Audio(),
      trackCount: 0,
      tracks: []
    };
  },
  methods: {
    getTracks() {
      axios
        .get(
          "https://api.napster.com/v2.1/tracks/top?apikey=ZTk2YjY4MjMtMDAzYy00MTg4LWE2MjYtZDIzNjJmMmM0YTdm"
        )
        .then((response) => {
          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();
      }
    },
    prevTrack() {
      if (this.trackCount >= 1) {
        this.trackCount--;
        this.setPlayerSource();
      }
    },
    setPlayerSource() {
      //this.player.src = this.tracks[this.trackCount].previewURL;
      console.log(this.tracks[this.trackCount].previewURL);
    },
    playPause() {
      this.player.play();
    }
  },
  created() {
    this.getTracks();
    this.setPlayerSource();
    this.playPause();
  }
};

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

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

.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;
  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://www.udiscovermusic.com/wp-content/uploads/2017/08/The-Smashing-Pumpkins-Mellon-Collie-and-the-Infinite-Sadness.jpg") center top;
  background-size: cover;
}

.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 {
  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">
      <i class="fa fa-volume-up"></i>
    </span>
    <div class="album">
      <div class="album-items">
        <div class="cover"></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}">
          <i class="fa fa-step-backward"></i>
        </div>
        <div class="navigate navigate-play" @click="playPause">
          <i class="fa fa-play"></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>

问题

由于我无法弄清楚的原因,我收到 Unhandled error during execution of created hook 错误。

我的错误在哪里?

您需要等待您的异步函数被解析。

async created() {
    await this.getTracks();
    this.setPlayerSource();
    this.playPause();
  }

并通过返回回调或使用异步等待使您的 getTracks 函数异步。

   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);
    }
}