Synchronizing Vuetify Carousel with dynamic route parameters

I'm facing an issue where the v-carousel value does not sync correctly with a route parameter using a computed property. Despite waiting for the mounted hook, the carousel seems to emit its own input and disregard the initial value from the route parameter.

For example, when the original route parameter is 1 (/test-route/1), it inexplicably changes to 0 (/test-route/0) after loading.

Is there a way to properly initialize and synchronize the carousel value with a route parameter?

<template>
  <v-carousel v-model="ind" >
    <v-carousel-item
      v-for="(item, i) in images"
      :key="i"
    >
      <v-img :src="item.image_thumb200" />
    </v-carousel-item>
  </v-carousel>
</template>

<script>
export default {
  props: ['images'],
  data: ()=>({
    // ind: 1,
    isMounted: false,
  }),
  watch: {
    "$route.params": {
      handler: function(val){
        console.log("route",val)
      },
      deep: true,
    }
  },
  computed: {
    ind:{
      get(){
        console.log('get: ',this.$route.params.id)
        return this.$route.params.id
      },
      set(val){
        if(this.isMounted){
          console.log('set: ',val)
          this.$router.push({params: {id: val}})
        }
      }
    }
  },
  mounted(){
    console.log('mounted')
    this.isMounted = true
  }
}
</script>

Here's the console output:

get:  1
mounted
set:  0
route {id: 0}
get:  0

Answer №1

After some troubleshooting, I finally solved the issue. It turns out that converting the route parameter to a Number was the key. Instead of simply retrieving the getter value like this:

get(){
  return this.$route.params.id
},

I needed to modify it to convert the parameter to a number before returning it:

get(){
  return Number(this.$route.params.id)
},

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

What is the best method for setting a session cookie securely while also using CSRF tokens?

In my express application, I am working on setting the session cookie to be secure. Here is the code snippet I have tried so far: app.use(express.cookieParser()); sessionOptions = definitions.REDIS; sessionOptions.ttl = definitions.session.expiration; app ...

Incorporating an Angular 2 Directive within the body tag of an

My goal is to create a custom directive that can dynamically add or remove a class from the body element in HTML. The directive needs to be controlled by a service, as I want to manage the visibility of the class from various components. Question: How ca ...

Learn how to automatically navigate to another page upon successfully creating an account and setting a value in the Firebase database

I am working on a system where, after successfully creating an account and setting a value in Firebase database, the user will be redirected to another page. Currently, I have managed to navigate to the next page but I am facing an issue with setting the ...

Issue arises when isomorphic-dompurify is used alongside dompurify in Next.js 13 causing compatibility problems

I am currently facing a compatibility problem involving isomorphic-dompurify and dompurify in my Next.js 13 project. It appears that both libraries are incompatible due to their dependencies on canvas, and I am struggling to find a suitable alternative. M ...

Applying a class to an element in VueJS is not functioning as expected

My goal is to assign the class .testcolor to the div element when testvalue is true, and apply no class when it's false. I encountered an issue where the getClass method does not get called when added to :class attribute, but works fine when called f ...

Updating the value of a global variable through dependency injection in AngularJS

One thing I consistently do is inject app.constant('DOCUMENT_ID', window.documentId || 1); into services, controllers, and directives. I'm now looking to convert DOCUMENT_ID into a global variable. The goal is for any changes made to DOCUME ...

The dropdown menu repeatedly opens the initial menu only

My script fetches data from a database to populate a table with one row for each member. Each row contains a dropdown list with the same class and ID. Although I attempted to open and close the dropdowns using specific codes, I am facing an issue where onl ...

"Looking for a solution to the ESLint error related to 'no-unused-var' and Google Maps integration. How can I effectively resolve

I'm struggling with what seems to be a simple problem I tried adding /* export myMap */ or /* global myMap */ at the beginning of the script but I keep getting errors Code HTML <h1>My First Google Map</h1> <div id="googleMap" ...

Ways to Increase jQuery Element ID

I have several instances of jPlayer (a jQuery audio player) set up: jPlayer1, jPlayer2, jPlayer3, jPlayer4. Each one is initialized and preloaded with audio files. The following function will loop through an existing array and attach a method to play the ...

Include HTML tag and class inside a JavaScript code block

A dynamic button is loaded when a certain condition in the JavaScript is met. Upon loading, I want to trigger a function (ClickMe) by clicking the button. However, I'm facing difficulty connecting the function with the button in my code. In my scrip ...

Turn only one bracket on the accordion

When clicking on a specific header, I want only one chevron to rotate instead of all the chevrons rotating. I am currently unsure how to specify which chevron should rotate individually. My project is in ASP.NET MVC 5 and I am using razor view to loop th ...

What is the process for testing and executing the code I've written on ACE Cloud 9?

I have integrated ACE on my website to enable users to code freely. My question is, how can I execute the Python code they write? I want to add a run button in the bottom left corner (which I can style using CSS), but I'm not sure how to actually run ...

The challenge of mapping React Select

I have implemented react-select in my project and I am using it within a map function like this: renderItems() { this.props.items.map(item => ( <Select id="options" value={this.state.optionSelected} onChange={this.onChangeOpt ...

Dynamically assigning a name to a variable through code

Is there a quicker way to complete these 100 tasks? variable_1 = 1; variable_2 = 2; variable_3 = 3; ... variable_100 = 100; I attempted to use the following code: for(var i = 1; i <= 100; i++) { variable_ + i = i; } However, I encountered an e ...

Auto-scroll feature malfunctioning

My auto scroll function using jQuery isn't working, here is my CSS: #convo_mes{ text-align:left; width:98%; height:80%; background:#fff; border:1px solid #000; overflow-x:auto; } And in my JavaScript: $(".mes").click(functio ...

Mapping JSON data containing a collection of objects as an observable, rather than as an observable array, is an

When developing my webpage, I needed to receive a viewmodel of a user account via Json. The data includes essential user details such as real name and email address. Additionally, I wanted to display (and modify) the groups that the user belongs to, along ...

How can a false validation be conducted on knockout js?

Using knockout js, I have an input type checkbox and I want to trigger the "not true" action when this checkbox is selected. Here's what I have attempted: <input type="checkbox" data-bind="checked: !IsVisible"/> Unfortunately, this code isn&ap ...

Unable to assign a value to a dropdown using Angular.js and PHP

I am encountering a slight issue with setting the data as a selected value in a dropdown after fetching it from the database. Below is an explanation of my code. plan.html: <div class="input-group bmargindiv1 col-md-12"> <span class="input-g ...

Prevent the screen from jumping to the top when re-rendering occurs

Is there a way to prevent Vuejs from scrolling to the top of the page after re-rendering a component? It can feel like refreshing the page. How can I keep the page fixed in its position? Imagine that after clicking on a certain part, the component is goin ...

Using Vue.js: Sending a Data Object to the Store via an Action

I am looking for a way to send a data object to my Vue store using an action. This is my current approach: <button type="button" name="button" class="btn btn-primary" @click="addToCart(row)">Add to cart</button> ...mapActions(["addToCart"]) ...