Update the Vuetify tab to correspond with either the route or query parameters

I am displaying tabs in this manner...

<v-tabs v-model="tab" v-on:change="tabbed()" fixed-tabs>
  <v-tab ripple>Tab A</v-tab>
  <v-tab ripple>Tab B</v-tab>
  <v-tab ripple>Tab C</v-tab>
  
  // tab items, etc

I want the current URL path to determine the active tab and for changing the tab to update the path accordingly. I attempted to achieve this using path (since I'm new), but instead used a query format like /mypath?tab=2

  data: () => ({
    tab: null
  }),
  methods: {
    tabbed () {
      console.log(`we switched tabs, now tab is ${this.tab} and route is ${JSON.stringify(this.$route.path)}`)
      this.$router.replace({ path: this.$route.path, query: { tab: this.tab } })
    }
  },
  created () {
    console.log(`current tab is ${this.$route.query.tab}`)
    this.tab = this.$route.query.tab
  }

I assumed this approach would work: retrieve the tab value from the query on page load and set it as the active tab. Upon switching tabs, update the query with the new tab value. However, when navigating to /mypath?tab=2, or any other tab, the page defaults back to the first tab and the console displays:

current tab is 2

we switched tabs, now the tab is 0 and route is "/mypath"

Is there some issue during initialization that resets the tab to zero??

Can I configure this functionality with routes, such as being able to visit "/mypath/tab-a" Alternatively, can I still make this feature function using queries? Despite what seems like correct code implementation, the reason behind its failure remains unclear.

Answer №1

Here is how I managed to solve the issue:

<template>
    <el-tabs v-model="activeTab">
        <el-tab-pane label="tab A" name="tabA"></el-tab-pane>
        <el-tab-pane label="tab B" name="tabB"></el-tab-pane>
        <el-tab-pane label="tab C" name="tabC"></el-tab-pane>
    </el-tabs>
</template>

<script>
    export default {
        data() {
            return {}
        },
        computed: {
            activeTab: {
                set(val) {
                    let query = {...this.$route.query};
                    query.tab = val;
                    this.$router.replace({query: query});
                },
                get() {
                    return (this.$route.query.tab || 'tabA');
                }
            }
        }
    }
</script>

This solution might be beneficial for your case.

Answer №2

Encountering the same issue, I found a solution by utilizing parseInt in managing the route:

this.section = parseInt(this.$route.query.section)

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 issue persists with react-hook-form and Material UI RadioGroup as the selected value remains null

Having trouble with RadioGroup from Material UI when using react-hook-form Controller. I keep getting a null selected value, even though my code seems right. Can you help me figure out what's missing? import * as React from "react"; import { ...

Leveraging jquery $.ajax when handling window.onerror in Internet Explorer

Currently, I have a script that is functioning correctly on Firefox and Chrome, but I do not prioritize Safari compatibility. This script is designed to send an email whenever a JavaScript error occurs. However, I am facing an issue with Internet Explorer ...

Uploaded images from mobile devices appear to be rotated when viewed on desktop browsers like Chrome and Firefox

I'm facing a strange issue where an image uploaded from a mobile device to my website appears rotated on Chrome and Firefox when viewed on a desktop. However, it displays correctly on mobile browsers like Chrome Mobile and Safari Mobile. It seems tha ...

What is the best way to retrieve data from multiple pages of a JSON API in one go?

Why am I only receiving the first page out of 61 pages in this json file with my current call? How can I retrieve all of them? Is there a way to use axios in a loop or another method? <template> <div id="app"> <the ...

Tips for updating form tag details within the same blade file upon reloading

I have set up a payment page that displays the total amount to be paid. To facilitate payments through a 3rd party gateway, I have utilized a form tag on the page. Now, I am looking to integrate a promo code feature on this payment page so that the total a ...

Is there a way to display my WhatsApp number, email, and location using icons?

[Apologies for my English not being perfect] I came across this image on and I'm curious about how to add similar WhatsApp, email, and location buttons on my own website. Can anyone assist me with this? Initially, I mistook these elements for images ...

EJS is refusing to render the HTML page

When utilizing Express with EJS to display the response from a database query as a basic Html page, an error is encountered in the development tools. "Access to fetch at 'https://o189131.ingest.sentry.io/api/5478725/envelope/?sentry_key=504fcb6 ...

When attempting to print a Rectangle on my webpage using JavaScript and taking user input, I encountered an error stating that 'str' is not defined

I'm trying to implement a feature where clicking on the "try it" button will prompt the user for the number of lines and then print that many lines in the form of a rectangle. However, when I run my code, nothing appears on the DOM and there is an err ...

Transform "<Mutation>" to useMutation

In my removeUser page, I have implemented a < Mutation > and handled errors using the submitForm() function. The initial code worked perfectly: export default function RemoveUserPage() { const [isSubmitted, setIsSubmitted] = useState(false); con ...

struggling to access the value of a hidden field by using the parent class name

My coding experience so far looks like this- <tr class="chosen"> <td id="uniqueID">ABCDE5678</td> <input type="hidden" value="000005678" id="taxCode"> <td id="fullName">Z, Y</td> </tr> In this scenario, I need to ...

Guide on incorporating a texture file (.mtl) onto a model (.obj) with three.js

I've managed to successfully display a .obj file using three.js by adapting code I found online for my project. However, I'm now facing challenges when trying to incorporate the .mtl material file. Despite attempting various solutions, nothing s ...

Function in nodejs throwing an error: Return type missing

I am facing an issue with this code snippet while trying to compile the application. public async test(options?: { engine?: Config }): Promise<any> { const hostel = new Service({ list: this.servicesList, createService ...

What is the process for generating an HTTP response that results in a pipe error being thrown

In my NestJS application, I have created a custom pipe that validates if a given value is a valid URL. If the URL is invalid, an error is thrown. This pipe is utilized in a controller to save an item into the database. Recently, I discovered that the pipe ...

Is it necessary for me to manually update the package.json dependencies versions after initiating a new project using npm?

I have recently set up a project with Electron and Vuetify frameworks integrated from this repository: https://github.com/vuetifyjs/electron Query: Is it recommended to manually update the versions in the package.json file to the latest and then execute n ...

Utilizing React Native to implement dynamic in-app badge notifications based on Firebase/Firestore data

My tech stack: React Native OneSignal for push notifications React Navigation React Native Elements (Badge component) React Native Vector Icons Firebase, Firestore as backend Within my app, I have two buttons located in the topRight Stack Screen. <Sta ...

Error: Unrecognized primary operator: $sortby

We have a web application built using Node.js and Mongoose, running on Ubuntu Linux servers hosted on DigitalOcean VPS. One of our Mongoose queries includes a text index with the following operators: less than / equal to limit order by This is how the ...

Decoding JSON object from the "string" using JSON.parse function

I am attempting to send a JSON string from a PHP controller to a twig template using the following method: $data['dist_result'] = json_encode($distribution_service->setDistribution($ids,$distribution)); $this->display('backend/shipmen ...

Problem with Material-UI Drawer

Is there a way to make this drawer stay fixed on the page like a sticker and remain active without moving when scrolling? I've tried using docked={false}, but it makes the whole page inactive except for the drawer. Any suggestions on how to solve this ...

Preventing v-stepper-step header click in Vuetify.js when form is invalid

I'm using Vuetify's v-stepper component. What I'm trying to achieve is: When a user clicks on a stepper step in the header, I want to block the click event if the active form is not valid. I have been able to accomplish this by attaching ...

A step-by-step guide on enabling Autoclick for every button on a webpage

I am currently using Jquery and Ajax to carry out an action, my goal is for a code to automatically click on every button once the page has finished loading. Although I attempted to use the following javascript code to achieve this at the end of my page, ...