0

Laravel Vue SPA: How to correctly retrieve a json object from Laravel and test print it out to the console.log() from within the Vue component?

The json object is correctly available at somedomain.com/channellist. It is then retrieved by axios.

Any help would be greatly appreciated.

Vue component:

<template>
    <div id="app">      
            <vue-chat :channels="channels"></vue-chat>  
    </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 object returned from Laravel at 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"}]}

1 Answer 1

1

Since you set the channels data in the promise you can loop through it in the template

Note: change channels: {} to array channels: []

<template>
    <div id="app">      
       <vue-chat :channels="channels"></vue-chat>
       <div v-for="channel in channles" :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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.