Guide for implementing tabs using router-view in Vue.js

I've been utilizing vuesax [ https://github.com/lusaxweb/vuesax ] for managing tabs. Within vs-tabs, I have multiple router-views. I am looking to display different Vue template files for each respective tab of the router-view.

Below is the code snippet from app.vue:

   <template>
   <div id="app">
  <div class="">
   <h3 style="color: #0DB26B;">
    home <mark class="red">New</mark> Design
  </h3>
<vs-tabs  :color="colorx" alignment="fixed" >
  <vs-tab label="Home" >
    <router-link
            tag="button" class="btn btn-link" to="/home">Home
          </router-link>
    <div class="con-tab-ejemplo">
      <router-view></router-view>
    </div>
  </vs-tab>
  <vs-tab label="Add Invoice" >
  <router-link
            tag="button" class="btn btn-link" to="/card">Card
          </router-link>
    <div class="con-tab-ejemplo">
      <router-view></router-view>

    </div>
  </vs-tab>
  <vs-tab label="Profile">
    <div class="con-tab-ejemplo">
      <router-view></router-view>

       </div>
      </vs-tab>
      </vs-tabs>
    </div>

  </div>
 </template>

Here is the router code from index.js:

   import Vue from 'vue'
   import Router from 'vue-router';
   import Home from '@/components/Home';

   import Card from '@/components/Card';


   Vue.use(Router);

   let router = new Router({


   routes: [
    {
        path: '/home',
        name:"Home",
        component: Home,

    },
    {
        path: '/card',
        name:"Card",
        component: Card,

         },

      ]


   });
  export default router;

My current issues:

1.) The same Vue template is being displayed in all vs-tabs router-views. How can I display different templates for each tab? 2.) How can I set router-links for vs-tabs?

Attached are screenshots for reference:

Any assistance on this matter would be greatly appreciated. Thank you.

https://i.sstatic.net/RSk53.png

Answer №1

To navigate between pages in Vue.js, you can utilize the $router.push method

   <div>
      <vs-tabs color="success" alignment="fixed">
        <vs-tab label="Home" @click="$router.push('/home')">
          <div class="con-tab-ejemplo">
            <router-view></router-view>
          </div>
        </vs-tab>
        <vs-tab label="Add Invoice" @click="$router.push('/card')">
          <div class="con-tab-ejemplo">
            <router-view></router-view>
          </div>
        </vs-tab>
        <vs-tab label="Profile"></vs-tab>
      </vs-tabs>
    </div>

Check out the example on codesandbox

Answer №2

Just a quick heads up, in vuesax v3, you won't be able to utilize to or :to. It seems that the vs-tab component does not support that prop. It only responds to @click events.

If you're looking to implement this with nested components, you can make use of nested routes. Let's consider a scenario where instead of working with the root component App.vue, we navigate to a child component named Main.vue through the URL /main. Your router.js file should resemble the following:

{
    path: '/main',
    component: Main,
    children: [
        {
        path: '',
        redirect: 'home'
        },
        {
        path: 'home',
        component Home
        },
        {
        path: 'card',
        component: Card
        }
    ]
}

When you visit /main, you will be redirected to /main/home, displaying the Home tab. Clicking on the Add Invoice tab will take you to the /main/card URL.

Remember to update the links in your template to correspond with these paths. Remove the / to have

@click="$router.push('home')"
and
@click="$router.push('card')"
.

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

Maintaining User Login State Across Route Changes in Vue with Vuex

Our small Dev-Team is excited to venture into creating our own social media website just for fun. We have implemented a login process using a jwt that is stored in localStorage. async login( { commit }, user) { commit('auth_request'); ...

The powerful combination of Nuxt, Vuex, and Computed Properties allows for

Currently exploring Nuxt.js with Vuex, I have created a basic form containing an email field, a password field, and a button. All my state, mutations, and actions are functioning correctly. However, I encountered an issue when trying to create a computed ...

What are the reasons that execCommand does not function correctly when attempting to justify text?

Why isn't justify with execCommand working properly? Take a look at the code below: $('#JustifyLeft').click(function(){ document.execCommand("JustifyLeft", false, ""); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2 ...

Ways to repair the mouse hover transform scale effect (animation included)

I am currently facing an issue with my GridView that contains images. When I hover over the top of the image, it displays correctly, but when I move to the bottom, it does not show up. After some investigation, I suspect that there may be an overlay being ...

Optimize your mobile experience by creating a notification menu that is scrollable and fixed in position

Recently, I purchased Moltran, but encountered a major issue: The notification menu disappears on mobile devices, which is not ideal for me. After some research, I discovered that removing the hidden-xs class from the li notification element can solve this ...

Is it possible to isolate specific HTML elements within a designated area?

Is it possible to identify which elements are located within a specific rectangular region on a web page identified by a URL? UPDATE: The default settings for screen resolution, font size, etc. can all be adjusted to reasonable values. ...

Passing data from Angular component (.ts) to its corresponding template (.html) file through ngOnInit() method

I have a task involving Angular 18 that requires updating the html page based on the response value of ngOnInit(). During testing, I noticed that the test and maxPage values of ngOnInit() displayed on the html component are test = "ngOnInit" and maxPage = ...

Aligning items in several columns

Setting up a grid layout where the content is centered can be tricky, but I've found a solution that works well. Take a look at the code snippet below: .outer { width: 100%; height: 100px; margin-top: 10px; m ...

Issue with orientation change when using webkit-overflow-scrolling: touch;

I am currently facing an issue with a fixed position div that has a specified width. The problem arises when the content is long enough to require overflow in one device orientation (landscape), but not the other (portrait) - scrolling stops working if the ...

Adding extra space to the list items in CSS causes the text to become static and unaffected by fluctuations in the line-height

Here's the problem I'm facing: I have a basic list consisting of a shopping cart, login and user registration. I want to adjust the position of the list by adding padding, but every time I do so, the line height also increases. Is there a workaro ...

Error in Serializer when validating data with Vue3 frontend and Django backend

I built a Vue front end that gathers user data and files, then uses Axios to POST it to a Django Rest Framework endpoint. Below is the function code: import { ref } from "vue"; import axios from "axios"; const fields = ref({ audie ...

What is the best way in jQuery to display a particular div with a unique id when a link is clicked?

I have a div with the following structure: <article id="#pippo">blablabla</article> which I have hidden using jQuery $('article').hide(); Now, I want to create a link menu that displays a specific article id when it's clicked ...

Is there a way to convert the existing JavaScript code for a hyperlink so that it can be triggered by clicking a button instead?

I have a JavaScript code that works well when the hyperlink below is clicked: <a href="delete_event.php?event_id=110" onClick="return ConfirmDelete()" class="list-group-item">Delete Event</a> <script> function ConfirmDelete() { var ans ...

Dragging and dropping an HTML canvas element causes its width and height to be reset to zero

I am currently developing a customizable dashboard that allows users to rearrange dashboard tiles (represented by div elements) and position them anywhere within the dashboard. HTML Structure The HTML structure is outlined in the snippet below: <div cl ...

Arranging Divs into Two Columns in CSS - A Simple Guide

I'm trying to display a variable number of divs across two lines, like this: [1] [3] [5] [7] [2] [4] [6] ... I've explored using the column-count property in CSS, but it doesn't quite fit my needs since it requires a fixed number of ...

Sharing information between different components in React can be done using props, context, or a

When attempting to edit by clicking, the parent information is taken instead of creating a new VI. I was able to achieve this with angular dialog but I am unsure how to do it with components. This is done using dialog: <div class="dropdown-menu-item" ...

Utilizing vue-touch-events in conjunction with vue-router

I attempted to implement vue-router along with vue-touch-events in the following manner: <i v-touch="go('home')" class="fas fa-bars"></i> <script> export default { name: "Nav", methods: { go: function(state) { ...

I'm puzzled as to why my Contact Form is failing to send out emails

Looking to create an email send function in a pop-up on my website that can be accessed via the following link: The issue I'm facing is that while it redirects perfectly to the thank you message, the PHP implementation does not seem to work after the ...

Is it possible to have more than one button for each item on my Shopping List App?

I am developing a shopping list app that allows users to add items to a list. Each item added triggers the display of a button next to it, enabling users to remove the item from the list when needed. However, a problem arises as more items are added – i ...

Arrange containers into a tower?

Currently, I am exploring the concept of stacking boxes. While I have a solid grasp on how to stack them vertically from top to bottom, I find myself puzzled about how to stack them horizontally. Check out my vertical stacking method here See how I a ...