Switching between links while using Vue Router may cause jQuery to become disabled

I am currently utilizing vue router to facilitate navigation on my website. Within this setup, I have various pages such as a home page and an about us page. The issue I am facing pertains to some jQuery functionality that is implemented in a separate JS file specifically for the home page. Strangely enough, when I navigate to another page (like the about us page) and then return to the home page, the script ceases to work. This inconsistency also occurs when I visit any other page before returning to the home page. My suspicion is that this problem is related to the vue router configuration. Here is the snippet of the jQuery script in question:

$('.arrow_down').click(function() {
    $('html, body').animate({
        scrollTop: $('.ourservices').offset().top
    }, 600);
});

Below is an outline of the Vue router structure being used in my project:

// Introducing some important components
var home = Vue.extend({
    template: '#home-template'
})

var aboutus = Vue.extend({
    template: '#aboutus-template'
})

var portfolio = Vue.extend({
    template: '#portfolio-template'
})

var contact = Vue.extend({
    template: '#contact-template'
})

// To initiate routing, you need a root component.
// In our scenario, we are opting for a simple approach
// using an empty component, since the app template is HTML-based.
// Please note that App isn't a Vue instance.
var App = Vue.extend({})

// Creating a new instance of the router.
// Additional options can be passed here if necessary,
// but simplicity is key for now.
var router = new VueRouter({
    hashbang: false,
    history: true,
    linkActiveClass: "active"
})

// Defining the routes.
// Each route should correspond to a specific component. The "component" can
// either be a custom component established via Vue.extend(), or merely a set of component options.
// More details on nested routes will be clarified later on.
router.map({
    '/': {
        component: home
    },
    '/aboutus': {
        component: aboutus
    },
    '/portfolio': {
        component: portfolio
    },
    '/contact': {
        component: contact
    }
})


// Ready to launch the application!
// The router will instantiate App and mount it to
// the element matched by the selector #app.
router.start(App, '#app')

If you require further information, please don't hesitate to ask.

Answer №1

To incorporate your script within component's ready function, follow this example:

var home = Vue.extend({
  template: '#home-template',
  ready: function () {
    $('.arrow_down').click(function() {
      $('html, body').animate({
        scrollTop: $('.ourservices').offset().top
      }, 600);
    }); 
  } 
}) 

Check out the sample fiddle here

You can also refer to the Vue Instance Lifecycle here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to remove a dynamic field with jquery

I have developed a script that allows me to add dynamic fields and delete them as needed. However, I am facing an issue where I cannot delete the first element with the "el" class in my script because it removes all elements within the "input_fields_cont ...

When you try to remove an element from an array by its index in Vue JS, the outcome may not be

Here is an array that I have: var hdr = ("name", "date", "start_time", "selling_item", "total_call", "end_time", "ad_num", "area", "order_num"); //this data comes from the database Now, I need to rename these array elements according to prope ...

I'm having an issue with my progress bar in ReactJS - it seems to move when I scroll the window instead of

My code includes a progress bar that works fine - it shows a movable progress bar when the window is scrolled. However, I want the progress bar to move when scrolling inside my div, not on the entire window. I am currently using jQuery for this functionali ...

What is the best method for preventing a form's ajax events from triggering when a link inside the form is clicked?

I am working with an ajax form that looks like this... <form action="/whatever" method="POST" id="blerg"> </form> There is an ajax event attached to it for when the form is submitted: $("#blerg").bind(&a ...

Interact with parent function from child component using data attribute in Vue.js

I have a parent component called Waypoint that contains child elements. I want to be able to call functions on these child elements from the parent component. How can I achieve this? //coding example //template <Waypoint> <div :data-wp-cb ...

substitute bullet point list item with new element

Assuming I have a list: <ul class="ul-1"> <li class="li-1">xx -1</li> <li class="li-2">xx -2</li> <li class="li-3">xx -3</li> </ul> Then I store it as: var list = $('.ul-1').html(); In ...

Tips for preventing a page from automatically scrolling to the top after submitting a form

Recently, I set up a form where users can input values. The form is set to submit either on change or when the user hits enter, depending on which value they want to update. However, after the values are submitted, the page automatically jumps back to the ...

The Bootstrap navbar stubbornly refuses to hide after being clicked on

Is there a way to adjust the data-offset-top for mobile view? Additionally, I am having trouble hiding the menu when clicking on a link. I have tried some code from stackoverflow without success. Can someone please assist with this issue: <nav class= ...

Trigger the click() event in jQuery before the blur() event

Looking to implement autocomplete suggestions in an input field? Take a look at the code below: <input type="text" id="myinput"> <div id="myresults"></div> To hide the results' div on the input's blur event, you can use this c ...

Is there a different method to retrieve the a.href of a list element using jQuery?

Below is the HTML code: <ul class="tabs"> <li><a href="#doc-new">Home</a></li> <li class="active"><a href="#doc-fav">Profile</a></li> <li><a href="#doc-read">Messages</a>& ...

React to the Vue: Only activate the event if the key is pressed twice consecutively

In my application, I am creating a unique feature where users can trigger a window to appear by inputting the symbol @ (shift + 50). This will allow them to access predefined variables... <textarea @keyup.shift.50="showWindow"></textarea> My ...

When attempting to access the state within an action, the state appears to be

Upon refreshing the webpage (with vuexPersistedState), I am trying to access my state within an action using the following code snippet: updateOuterValue: ({ commit }, data) => { console.log(state); ... } Despite having ...

Understanding the process of parsing JSON response using JavaScript

I am facing an issue with reading a JSON object in JavaScript. I have received this JSON object as a response and now I need to create a jstree based on it. Here is my JavaScript code: var repoId = $('#frmHdnV').val(); // variable to hold req ...

Having an issue with Vue Dev Tools where the "Open in Editor" button is not functioning properly. Any suggestions on how to resolve this?

I followed a tutorial to implement a new feature, using the instructions found here: https://github.com/vuejs/vue-devtools/blob/master/docs/open-in-editor.md However, I encountered an error: "C:\Users\User\AppData\Local\Programs& ...

Using app.js in a blade file can cause jQuery functions and libraries to malfunction

In my Laravel application, I am facing an issue with my vue.js component of Pusher notification system and the installation of tinymce for blog posts. Adding js/app.js in my main layout blade file causes my tinymce and other jQuery functions to stop workin ...

The function does not generate an image, but it will display the one that is

It looks like there may be an issue within the function that is preventing the image from being created, or possibly in the script itself which is not passing the image to the function properly. Below is the upload script (please note: this script is inje ...

"Centered in the middle of the screen, an animated

Encountering an issue with this code on Chrome. Clicking on the checkbox causes the text/checkbox to shift in position/padding/margin unexpectedly.. :( View the code on JSFiddle HTML <input id="inp" type="checkbox" /> <div id="cb" class="inp_ch ...

Ways to display jQuery values as HTML text

Trying to concatenate the HTML text with the values of item.certificate_name has been a challenge for me. I've experimented with various methods, but none of them seem to work. The specific line I'm referring to is where I wrote . I have alread ...

The functionality of the code in a stack snippet may differ from that in a standalone HTML file

My code works perfectly on a stack snippet, but when I insert it into my server or an .html file, the refresh button shrinks! I have copied and pasted the code exactly as it is. Are there any specific snippet features that need to be added for it to work, ...

Parsing text files with Highcharts

As a beginner in JavaScript and HighCharts, I am facing a simple problem that has me completely lost. My goal is to generate a scatter chart with three lines by reading data from a text file formatted like this: x y1 y2 y3 1.02 1.00 6.70 ...