Enhance your Vue router's scroll behavior with tips on navigating route values

I am working on a route that includes various parameters and queries:

For the parent route: path: "/:locale(en|jp)?" And for the products route:

path: 'products/:category/:page?'
The query in product may include:

{
  q: search string for filtering,
  size: size value for filtering,
  color: color value for filtering,
  designer: designer value for filtering,
  sort: sorting value
}

An example of the application can be viewed here

My question pertains to changing the size, color, or designer, where I would like to return savedPosition from scrollBehavior. In all other cases, it should return {x:0,y:0}

I am wondering if there is a way to achieve this without having to check all query values from to and from within the scrollBehavior function, as it could unnecessarily complicate things. Instead, is it possible to pass a hint when pushing the route from the function that sets the color, size, or designer?

Answer №1

In order to simplify the logic in scrollBehavior, I made it return {x:0, y:0} repeatedly and came up with the following function:

const changeRoute = (route, component, maintainScrollPosition = true) => {
  const position = {
    top: window.scrollY,
    left: window.scrollX,
  };
  component.$router.push(route);

  if (maintainScrollPosition) {
    Promise.resolve().then(
      () => { window.scrollTo(position); },
    );
  }
};

I trigger this function from my component like so:

changeRoute(
  {
    ...this.$route,
    query,
  }, this, true,
);

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

The toggle button for columns is not triggering the callback action

When working with the following JSFiddle, I noticed that the action function does not seem to fire whenever a button to select a column in the column visibility tool is selected. Check out the code snippet below for reference: $(document).ready(function( ...

Issue with date and time range in SQL query when using PHP AJAX

I've been struggling with a major issue for the past 4 days, and I'm really hoping to find a solution here. After countless unsuccessful attempts, I've turned to this platform for help. The problem at hand involves using a SQL Server databa ...

Encountering a 500 internal server error after deploying due to utilizing getServerSideProps in Next.js

When trying to redeploy my website on Vercel, I added getServerSideProps to my index.js file. However, I encountered an error that I am not familiar with. The program works perfectly on localhost. This is the getServerSideProps code that I added: export a ...

Is there a different way to retrieve data in Node.js instead of using mysqli

<?php $connect = mysqli_connect('localhost', "root", "", "test"); if(!$connect){ die(mysqli_connect_error()); } $sql = "SELECT * FROM articles"; $result = mysqli_query($connect, $sql) or die ...

Using a loop to execute Javascript Promise.all()

I am currently facing an issue where I need to make a web API call twice inside a loop, and then wait for the results before pushing them into a larger array as subarrays. The code snippet below illustrates my approach: var latlngPairs = []; function extra ...

Is there a viable substitute for websockets that can be utilized on shared hosting platforms?

Are there any viable alternatives for websockets that can be used with shared hosting? While I'm aware of node.js, socket.io, and Express.js, I'm unable to use them in a shared hosting environment. If there are other options available for creatin ...

The manipulation of Analytics with artificial pageviews is altering the source of traffic for PPC campaigns

Currently, we are facing some challenges with a website called . It appears that only the PPC traffic source is affected by changes caused by the pageviews being pushed through to analytics. Since the website operates on Ajax, we need to utilize Javascrip ...

Experiencing a problem with loading scenes on a splash screen using WebGL and three.js

As a newcomer to SOF, I apologize for not being familiar with local customs. // Hello all! I am attempting to develop a 3D model with a splash screen and first-person movement using three.js and its examples. // Here is the source: // The issue at hand ...

Unlocking bundles of worth through javascript coding

Is there a way to retrieve a set of values using javascript? Upon page load, I am looking to display an alert showing the value '0-1' for any checked checkboxes. View example here var checkBoxes = document.getElementsByName('mailId[]&apos ...

HTML forms default values preset

I need help with pre-setting the values of a dropdown menu and text box in an HTML form for my iPhone app. When the user taps a button, it opens a webview and I want to preset the dropdown menu and text field. Can someone guide me on how to achieve this? ...

Troubleshooting Failures in RequireJs Optimization for Windows

Currently working on optimizing a requirejs-based project on a Windows system. I have placed the r.js.cmd in the Scripts folder, along with the nodeBuild.js file containing the configuration settings. ({ baseUrl: ".", paths: { jquery: "empty:" }, name ...

The strict-origin-when-cross-origin policy is enforced when submitting a POST request through a form to a specific route

Currently, I am diving into the world of Node.js, express, and MongoDB by reading Greg Lims's book. However, I've hit a roadblock when trying to utilize a form to submit data to a route that should then output the body.title of the form in the co ...

Preventing default form submission in jQuery: How to cancel it when a certain condition is met

I have a contact form where I validate the input values upon clicking on the submit button. If there is at least one empty input, I trigger an alert and prevent the form submission by using preventDefault. However, if all inputs are filled and submitted, t ...

VueJs has the ability to display/render a single object from a collection of hundreds of objects at a time

When my API returns hundreds of objects in a response, I want to display only one object at a time on a page. The page includes two buttons - Previous and Next - that allow users to navigate between objects. How can I efficiently manage this data retriev ...

Validating Presence of a Value in an Array Object Using AngularJS

var SelectedOptionId = 957; $scope.array = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}] Is there a method to verify the existence of a value within an array of objects like this using Angular and underscore? I've att ...

What is the best way to clear radio button selections in a form using reactjs?

I designed a survey form with 4 radio buttons for a single question. I also included buttons to submit the form and clear input fields. However, when I click on "Clear Input," the checked radio buttons do not get cleared. How can I achieve this using the r ...

Instafeed running on Ionic/AngularJS is unable to reach the scope

I have a question regarding the behavior of code in Ionic. I have added the following snippet to my controller in AngularJS and it works perfectly fine in pure AngularJS. However, in Ionic, the same code snippet does not work as expected. The tagName prope ...

Cloning jQuery with varied PHP array value

So, I have the given PHP values: PHP: <?php $names = array("Mike","Sean","Steve"); ?> <script type="text/javascript"> var name_data = <?php echo json_encode($names); ?>; </script> <div class="container"> <div cl ...

javascript: extracting class name from HTML elements

To extract class names from an HTML code using JavaScript, I can utilize the following method: var cPattern = new RegExp(/class[ \t]*=[ \t]*"[^"]+"/g); var cMatches = data.match(cPattern); However, the above code returns an array with values li ...

How can I use Vue.js @scroll to create a dynamic CSS property?

I am developing a vuejs component for my project and I am looking to implement a zoom functionality on scroll within a div, similar to Google Maps. <div @scroll="zoomOnScroll"> <Plotly :data="info" :layout="layout" :display-mode-bar="false"&g ...