Send a property parameter to the Vue component

In my project using Vue and Laravel, I am trying to create a modal window with 2 tabs. The idea is to have two buttons, with each button linked to a specific tab that should open when clicked.
These buttons are located in the blade template:

<button @click="openModal('login')">
<button @click="openModal('register')">

//The Vue component is also included in the blade template
<auth-component :show="showModal" :active_tab="activeTab" @close="showModal = false"></auth-component>

/resources/assets/js/app.js:
My goal is to pass a parameter to a component's openModal() method...

const app = new Vue({
    el: '#app',
    data: {
        showModal: false,
        activeTab: '',
    },
    methods: {
        openModal(tab) {
            this.activeTab = tab;
            this.showModal = true;
        }
    },
});

... and then use that parameter as a Vue prop to set a specific bootstrap-tab as active within the Vue component:

AuthComponent.vue:

<div class="modal-mask" @click="close" v-show="show">
    <div class="modal-wrapper">
        <div class="modal-container" @click.stop>
            <b-card no-body>
                <b-tabs card>
                    <b-tab title="Login" :active="active_tab === login">
                    <b-tab title="Register" :active="active_tab === register">
                </b-tabs>
            </b-card>
        </div>
    </div>
</div>
...
props: ['show', 'active_tab'],

However, the current code is not functioning as intended. There are no errors displayed in the console, but only the first tab (login) is shown as active after opening the modal.


UPDATE: I made changes to

<b-tab title="Login" :active="active_tab === login">
<b-tab title="Register" :active="active_tab === register">

Upon inspection in the Vue console, I can see that the required tab has an active: true property. However, even with this property set, the content of the Register tab remains hidden with a display: none style. It appears that simply setting active to true is not enough to make the tab content visible.

Check out the JSFiddle component code here

Answer №1

One way to handle this is by encapsulating them within

<b-tabs v-model="tabIndex"></b-tabs>
and then implementing a computed method that calculates the index based on the passed property.

UPDATE

computed: {
  tabIndex: {
    get: function() {
      if (this.currentTab === 'login') {
        return 1;
      } else {
        return 0;
      }
    },
    set: function(newValue) {
      return newValue;
    }
  }
}

Answer №2

To create a function with the bound parameters instead of invoking it directly, try passing

openModal.bind(undefined,'login')
.

The tabs component is configured with the v-model attribute, using numeric indexes to determine the active tab.

You could simplify things by creating a lookup object like this:

const tab_map = { login: 0, register: 1 }

Then you can set up

openModal.bind(undefined, tab_map.login)
.

For guidance on setting up v-model on tabs, refer to this example:

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

Color in the diamond shape only to a designated height

I am in search of a unique diamond shape that allows me to fill the color up to a specific height based on an attribute or variable provided. https://i.stack.imgur.com/62U6h.png. I attempted creating the diamond shape and initially considered placing it ...

Redux/React project bundle

I am currently attempting to incorporate the package https://www.npmjs.com/package/is-url into my React/Redux project, but I'm unsure about how to go about importing it. Are there any other ES6-compatible installation options that you would recommend ...

Troubleshooting issue with setting variables in Firefox's JavaScript

I have created a small JavaScript script that defines a value (referred to as stock), which I want to set as the maximum in a dropdown list. It will be set as the maximum value if it exceeds 6. Below is the code snippet: <script type="text/javascript" ...

Guy discussing the ins and outs of JavaScript variables

I am facing an issue with retrieving the value in the jQuery script. The value should be taken from route_path_images, but it's not showing up. var route_path_images="http://www.domain.com" jQuery("#header_background_night_star").fadeIn(2000).css("b ...

Updating JSON data using fetch API

Hi everyone, I'm currently working on an email application and need help with implementing an archive button for each email. It seems that the function to change the archived status to true is being called, but for some reason, it's not making th ...

Save the value of a webpage element into a variable and utilize it across multiple JavaScript files in TestCafe

We are working in the insurance domain and have a specific scenario that we want to achieve using TestCafe: 1st step: Login into the application 2nd step: Create a claim and store the claim number in a global variable 3rd step: Use the globally declared c ...

Navigate to the homepage section using a smooth jQuery animation

I am looking to implement a scrolling feature on the homepage section using jquery. The challenge is that I want the scrolling to work regardless of the page I am currently on. Here is the HTML code snippet: <ul> <li class="nav-item active"> ...

using an array as an argument in the filtering function

Is there a way to pass an array to the filter method in JavaScript? I have successfully filtered an array using another array. However, my filter array currently has a global scope. How can I pass the array to make my code cleaner? var array = [1, 2, 3, ...

Issue with Firebase REST API filtering functionality not functioning as expected

Trying to master making rest calls using firebase Here's my attempt, but it's not returning any results: ('/cases.json?orderBy="case_status"&equalTo="live"') When I remove the ?orderBy="case_status"&equalTo="live"' I ...

Utilize dynamic tags to implement filters

Currently, I am working on a project where I have a table displaying elements using ng-repeat. My goal is to implement dynamic filters that can be added through tags using ng-tags-input plugin. These tags will serve as the filter criteria for the table dat ...

Dealing with multiple Datepicker components in React hooks

Managing a single instance of Datepicker in a React project is easy, but what about handling multiple instances? The date changes are not reflected on the UI even though they can be seen in the console. What are some efficient solutions to tackle this issu ...

In Vue, applying CSS styles to D3's SVG elements may not work as expected when using the scoped attribute

Here is the code snippet of my component: <template> <div id="something" class="card"> </div> </template> const height = 200; const width = 200; let graph = d3 .select('#something') .append('svg') ...

What is the best way to retrieve a string from a URL?

Is there a way to extract only a specific string from a URL provided by an API? For instance: I'm interested in extracting only: photo_xxx-xxx-xxx.png Any suggestions on how to split the URL starting at photo and ending at png? ...

Choose to either check or uncheck boxes using ReactJS

After successfully creating a function to select either single or multiple boxes, I encountered an issue with the "Select all" feature. Any suggestions on how to resolve this? (I'm utilizing the material-ui library for my checkboxes, which are essenti ...

Guide on implementing the recently established attribute ID using jQuery

In my code snippet, I am assigning a new id to a button. Here is the code: $("#update").click(function(){ $("#update").attr('id', 'cancel'); }); After changing the id, I want to make it functional again in jQuery by reverting it b ...

Is there a way to use jQuery to make a div fade in and toggle over another

I have created a test page and I am looking to achieve a specific effect when the page loads. I want everything on the page to be hidden initially. When I click on the "About" text, I want it to fade in using fadeToggle(); however, when I click on "My work ...

Is it a cookie-cutter function?

Can someone help me solve this problem: Implement the special function without relying on JavaScript's bind method, so that: var add = function(a, b) { return a + b; } var addTo = add.magic(2); var say = function(something) { return something; } ...

What is the syntax for requesting a JSONArray in an Ajax call using jQuery?

After browsing through numerous resources like this, this, and this, I finally landed on this. The main objective is to iterate over the JSON data returned from an Ajax call, which is encoded using json_encode in PHP. When I inspect the object using cons ...

Is it possible to use Material-UI Link along with react-router-dom Link?

Incorporating these two elements: import Link from '@material-ui/core/Link'; import { Link } from 'react-router-dom'; Is there a method to combine the Material-UI style with the features of react-router-dom? ...

The interface vanishes upon the integration of TinyMCE into the module

Currently, I am working on a project using Angular-fullstack and attempting to integrate ui-TinyMCE. However, I encountered an issue when I made the following changes: angular.module('academiaUnitateApp') .controller('NewEntryCtrl', ...