0
부모 구성 요소에 axios 패키지를 사용하여 ajax 요청을 만들고 반환 된 배열을 자식 구성 요소로 전달하여 가능한 한 배열을 반복하고 레이아웃을 작성하십시오. 다음 코드가 있습니다.Vue.js : API에서 데이터 가져 오기 및 자식 구성 요소에 전달 (간단한 예제)
이<script>
import Box from './Box';
import axios from 'axios';
export default {
name : "messagespainel",
data: function() {
return {messsages: []} //should I use data to pass the ajax returned array to the child
},
props: ['messages'] //should I use props to pass the ajax returned array to the child
}
mounted :
{
axios.get('http://www.omdbapi.com/?s=star&apikey=mykey').then(response => { this.messsages = response.data.Search});
}
</script>
<template>
<div>
<box messages="this.messages"></box> My mind bugs at this point. How to refer to messages?
</div>
</template>
아이는 이것이다 :
부모가 이것이다
부모에<script>
export default {
name: "box",
props: ['mensagens']
}
</script>
<template>
<div>
<div v-for="message in messages" >
{{ message}}
</div>
</div>
</template>
<style>
</style>
답변이 매우 명확했습니다. –
하위 항목에서 div의 속성을 어떻게 데이터 바인딩 할 수 있습니까? {{property}}이 (가) 작동하지 않습니다! –
@DiegoAlves 자식이 루프에서 '{{message.property}}'일 것입니다. 루프가 메시지 "'에서'v-for ="메시지라고 가정합니다. – Bert