3

The parent app has an object messages, that is being filled correctly from the server. But the chat-room component's props messages is not feeding from the parent's messages. What am I missing??

Here is my blade template:

<chat-room></chat-room>
<chat-composer v-on:messagesent="addMessage"></chat-composer>

Here is my chat-room component:

<template>
  <div class="chat-room">
    <chat-message v-for="message in messages" :message="message"></chat-message>
  </div>
</template>

<script>
  export default {
    props : ['messages'],
  }
</script>

Here is my app.js:

Vue.component('chat-message', require('./components/ChatMessage.vue'));
Vue.component('chat-room', require('./components/ChatRoom.vue'));
Vue.component('chat-composer', require('./components/ChatComposer.vue'));

const app = new Vue({
  el: '#app',
  data: {
    messages: []
  },
  methods: {
    addMessage(message) {
      this.messages.push(message);
      axios.post(base_url+'/room/1/write_message', message).then(response => { });
    }
  },
  created() {
    axios.get(base_url+'/room/1/messages').then(response => {
      this.messages = response.data;
      console.log(this.messages); //this returns an Array[4]!
    });
  }
});
3
  • Can you show your code where you use your chat-room component? Commented Feb 19, 2017 at 2:38
  • Yeah, I got that you'd shown the contents of your ChatRoom.vue file. What I meant was can you show how you're using that component in you app i.e. where/how you're using <chat-room></chat-room>? Commented Feb 19, 2017 at 2:46
  • I updated the question. I'm using a simple html tag Commented Feb 19, 2017 at 2:49

1 Answer 1

11

The reason you're not seeing the messages inside your chat-room component is because you're not passing them to it.

Change:

<chat-room></chat-room>

To be:

<chat-room :messages="messages"></chat-room>

Hope this helps!

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.