0

Component

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

<script>
    export default {
        props: ["messages"]
    }

</script>

<style lang="css">
    .chat-log .chat-message:nth-child(even) {
        background-color: #ccc;
    }
    .chat-log {
        overflow-y: auto;
        max-height: 400px;
    }

</style>

When I change the above script code to below. I get errors..

<script>
    export default {
        props: ["messages"]
    },
    created() {
        $(".chat-log").scrollTop($(".chat-log").prop('scrollHeight'));
    }

</script>

Error Details

Unexpected token, expected ;

Issue comes only when adding the created method, Am I missing anything?

2 Answers 2

1

The created lifecyle method goes within the body of the Vue component itself, not outside. I mean:

export default {
    props: ["messages"],
    created() {
        $(".chat-log").scrollTop($(".chat-log").prop('scrollHeight'));
    }
 }

Vue.js Lifecycle

Sign up to request clarification or add additional context in comments.

2 Comments

I was actually looking for a method that executes once the component is fully loaded. but seems like created() will not help. can u suggest something?
@Pankaj try to take a look here. I think you'd go with mounted.
1

Your created(){} method should be encapsulated within your export default {} block.

In other words, change your code this:

export default {

  props: ["messages"],

  created() {
   $(".chat-log").scrollTop($(".chat-log").prop('scrollHeight'));
  }

},

3 Comments

I was actually looking for a method that executes once the component is fully loaded. but seems like created() will not help. can u suggest something?
@Pankaj That is what created does, it will only run once, same as mounted(){}
Check this vuejs.org/v2/guide/instance.html#Lifecycle-Diagram maybe you are looking for beforeCreate()

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.