I have a router link in my site header and when clicked goes to the page /registration
which has a form on it. What I would like to achieve is when clicked it scrolls to the reg form using vueScrollTo plugin.
It's working now but not 100% how I would prefer it to behave. As of now when not on the /registration
page and clicked it scrolls perfectly to the form however when I am on the /registration
page itself and click the router link I get a warning
trying to scroll to an element that is not on the page : undefined
I believe this is happening because I am firing the event on the mounted lifecycle. Is there a better solution to have this work properly?
Site header component
<template>
<router-link to="/registration"
class="nav-row--primary__button"
@click.native="scrollToRegForm()"
>
Sign up
</router-link>
</template>
<script>
export default {
mounted() {
if (window.location.href.indexOf("/registration") > 0) {
const regForm = document.getElementById("regForm");
setTimeout(() => this.scrollToRegForm(regForm), 1);
}
},
methods: {
scrollToRegForm(element) {
VueScrollTo.scrollTo(element, 1000);
}
}
}
Registration page component
<div class="container--content spacing-ie" id="regForm">
<RegistrationForm />
</div>