Laravel Vue SPA: Как можно передать опору, которая представляет собой массив, полученный axios
, дочернему компоненту?
Объект json правильно доступен в somedomain.com/channellist
. Затем он извлекается axios
, а затем передается дочернему компоненту.
Как его можно передать дочернему компоненту, чтобы activeChannel
правильно отображался?
Любая помощь будет будут очень признательны.
Vue дочерний компонент:
<template>
<div id="app">
<p>{{ activeChannel }}</p>
</div>
</template>
<script>
export default {
props: ['channels'],
data() {
return {
activeChannel: this.channels[0].id,
}
},
methods: {
},
components: {
},
created() {
}
}
</script>
<style>
#app {
margin-left: 1em;
}
.heading h1 {
margin-bottom: 0;
}
</style>
Vue родительский компонент:
<template>
<div id="app">
<vue-chat :channels="channels"></vue-chat>
<div v-for="channel in channels" :key="channel.id">
<label>Channel Name</label>
<input type="text" readonly :value="channel.name">
<br>
<label>Channel Name</label>
<input type="text" readonly :value="channel.created_at">
</div>
</div>
</template>
<script>
export default {
data() {
return {
channels: [],
}
},
methods: {
getChannels(){
this.loading = true
var url = "/channellist"
axios
.get(url)
.then(response => (this.channels = response.data.channels))
},
},
watch: {
},
components: {
},
created() {
this.getChannels()
}
}
</script>
<style>
#app {
margin-left: 1em;
}
.heading h1 {
margin-bottom: 0;
}
</style>
json объект, возвращенный из Laravel в somedomain.com/channellist
:
{"channels":[{"id":1,"name":"channel 1","created_at":"2020-07-10T06:03:14.000000Z","updated_at":"2020-07-10T06:03:14.000000Z"},{"id":2,"name":"channel 2","created_at":"2020-07-10T06:03:14.000000Z","updated_at":"2020-07-10T06:03:14.000000Z"},{"id":3,"name":"channel 3","created_at":"2020-07-10T06:03:14.000000Z","updated_at":"2020-07-10T06:03:14.000000Z"}]}