vue 组合中的反应性数据 api 未在模板中呈现
Reactive data in vue composition api is not rendered in template
我希望我能找到为什么这不起作用的答案。我正在尝试从 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>
使用此代码,我可以在控制台中看到获取的 'songs',但它不会呈现在页面中。我试过 onBeforeMount、onRenderTriggered、onUpdated 但 none 正在工作。我似乎无法弄清楚这一点。
reactive
应该有一个对象作为参数:
<template>
<div v-for="song in state.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 state= reactive({songs:[]})
onMounted(()=>{
axios
.get(props.carouselUrl)
.then(response => {
state.songs = response.data.results[0].category_tracks
console.log(state.songs)
})
})
return {
state,
}
},
</script>
我希望我能找到为什么这不起作用的答案。我正在尝试从 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>
使用此代码,我可以在控制台中看到获取的 'songs',但它不会呈现在页面中。我试过 onBeforeMount、onRenderTriggered、onUpdated 但 none 正在工作。我似乎无法弄清楚这一点。
reactive
应该有一个对象作为参数:
<template>
<div v-for="song in state.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 state= reactive({songs:[]})
onMounted(()=>{
axios
.get(props.carouselUrl)
.then(response => {
state.songs = response.data.results[0].category_tracks
console.log(state.songs)
})
})
return {
state,
}
},
</script>