【发布时间】:2021-03-05 21:07:16
【问题描述】:
我希望我能找到为什么这不起作用的答案。我正在尝试从 api 获取数据并将其显示在 vue 模板上。这是我的代码(摘要)。
<template>
<div v-for="song in songs" :key="song.id" class="custom-card" @click="addAudio($event, song)">
{{ song }}
</div>
</template>
<script>
const axios = require('axios').default
import { reactive, onMounted } from 'vue'
export default {
name: 'SongCarousel',
props: {
carouselUrl: String,
},
setup(props) {
let songs = reactive({})
// const data = localState.value
onMounted(
axios
.get(props.carouselUrl)
.then(response => {
songs = response.data.results[0].category_tracks
console.log(songs)
})
)
return {
songs,
}
},
</script>
使用此代码,我可以在控制台中看到提取的“歌曲”,但它没有呈现在页面中。我已经尝试过 onBeforeMount、onRenderTriggered、onUpdated,但都没有工作。我似乎无法弄清楚这一点。
【问题讨论】:
标签: javascript vue.js vuejs3 vue-composition-api vue-reactivity