Today I Learned - Rocky Kev

TIL how to jump to a element within a Vue Component

POSTED ON:

TAGS:

I couldn't figure out how to jump to an element within Vue.
After a lot of trial and error, this worked!

Use the this.$el.querySelector('.message') to select elements within a component.

I also included the new scroll-margin-top so the jump is so much more nicer.

<template>
<div>
<h1 class="message">Hi there</h1>
<component @click="scrollToMyElement" />
</div>
</template>

<script>
export default {
props: {
},
data: function () {
return {}
},
methods: {
scrollToError() {
this.$el.querySelector('.message').scrollIntoView({ behavior: 'smooth' });
},
}
}
</script>

<style>
.message {
scroll-margin-top: 20ex;
}
</style>

Related TILs

Tagged:

TIL How to implement two-way binding without a framework

One of the reasons we use React or Vue is because of that sweet sweet two-way binding. It's the automatic syncing of data between the model (data) and the view (UI) in both directions. Any changes made to the data will automatically update the view, and any changes made to the view will automatically update the data.

TIL Cherry-picking out of bootstrap-vue

If you are using a component library that uses ES modules like bootstrap-vue, you can cherry pick its components instead of just registering all of them globally.

TIL how Svelte is different fom Vue/React