1

First time with vue. I am learning it playing around with some examples from Laracasts. I cannot get external template to render and the console shows cannot find element: #toolbar-chat.

My template is:

<template>
    <div id="toolbar-chat">
        <div class="toolbar-chat">
            <ul v-for="chat in messages">
                <li><b>@{{ chat.nickname }} says:</b> @{{ chat.message }}</li>
            </ul>
        </div>
        <div class="input-group input-group-sm">
            <input class="form-control" value="" placeholder="Type message..." required="required"  maxlength="140" v-model="newMsg">
            <div class="input-group-btn">
                <button class="btn btn-primary" type="button" @click="press">
                    <i class="fa fa-paper-plane"></i>
                </button>
            </div>
        </div>
    </div>
</template>


<script>
export default {

    mounted() {
        console.log('Component mounted.')
    },

    data() {
        return {
            nickname: [],
            message: ''
        }
    },

    ready() {
        Echo.channel(chat_channel)
            .listen('ChatMessageWasReceived', (data) => {
                // Push data to messages list.
                this.messages.push({
                    message: data.chat.message,
                    nickname: data.player.nickname
                });
            });
    },

    methods: {
        press() {
            // Send message to backend.
            this.$http.post(chat_send_route, {message: this.newMsg})
                .then((response) => {
                    // Clear input field.
                    this.newMsg = '';
                });
        }
    }
};
</script>

My HTML contains the following tag:

<div class="col-xs-12 col-md-4" id="toolbarChat">
    <my-chat></my-chat>
</div>

My vue component call is inside a document ready function like this:

require('./../app/bootstrap');

$(document).ready(function()
{
    ....
    // Set up chat
    Vue.component('my-chat', require('./../generic/chat.vue'));

    const app = new Vue({
        el: '#toolbar-chat'
    });
});

And I include vue in my bootstrap file like this, then compile with webpack and no errors.

window.Vue = require('vue');

Why is my HTML template not rendering?

5
  • Couple things. Where is new Vue(...)? Also, ready() is a Vue 1 lifecycle event and doesn't exist in Vue 2. Commented Aug 11, 2017 at 2:52
  • Chang the id in the HTML to toolbar-chat not <div class="col-xs-12 col-md-4" id="toolbarChat">. Commented Aug 11, 2017 at 3:06
  • Hi @Bert, thanks! I was missing from my Q the new vue and I have added it. I will remove removed ready event as you mention. I can now see an error template not found #toolbar-chat so it is not pulling in the template still. Commented Aug 11, 2017 at 3:06
  • See above comment. Commented Aug 11, 2017 at 3:07
  • Got it, thanks! Pls answer the Q and I will upvote! Commented Aug 11, 2017 at 3:08

1 Answer 1

2

In your HTML you have the following div:

<div class="col-xs-12 col-md-4" id="toolbarChat">
    <my-chat></my-chat>
</div>

Change it to

<div class="col-xs-12 col-md-4" id="toolbar-chat">
    <my-chat></my-chat>
</div>

Because that is the id that new Vue({el: "#toolbar-chat",...}) is looking for.

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.