0

I am currently working on a chat and want a function that scrolls down to end of the div after data is added to an array.

updated() pretty much works fine but we also have a vue-timepicker in the component which displays a timer so the $nextTick() gets executed every second and you cannot scroll up.

Any ideas how to execute the $nextTick() only when the array with messages changes or any other thoughts?

updated() {
        this.$nextTick(() => this.scrollToBottom());
    },

2 Answers 2

1

You need to watch the exact property that changes

watch: {
  value(val) {
    // executes when value changes
    this.$nextTick(() => this.scrollToBottom());
  },
},

Here you can read more about it https://v2.vuejs.org/v2/api/#watch.

Note that updated is discouraged for this use case:

To react to state changes, it’s usually better to use a computed property or watcher instead.

https://v2.vuejs.org/v2/api/#updated

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

Comments

1

You could watch that array like :

watch:{
    arrName:{
      handler(newVal,oldVal){
          this.scrollToBottom()
      },
     deep:true

   }

}

2 Comments

Thanks for the answer, the problem with this solution is that it only scrolls down to the penultimate message and not the last.
please provide more details in your question and add a code to debug

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.