What is the best way to dynamically add a class to the right navigation element in Vue.js when the :class binding only accepts boolean values?

My issue involves implementing a fixed navigation bar with the following structure:

<nav class='navigation'>
    <div :class="{ active: }" @click='scrollTo(".test1")'></div>
    <div :class="{ active: }" @click='scrollTo(".test2")'></div>
    <div :class="{ active: }" @click='scrollTo(".test3")'></div>
    <div :class="{ active: }" @click='scrollTo(".test4")'></div>
</nav>

Furthermore, I have defined a property named activeSection in my code:

data() {
    return {
        activeSection: test1 // Currently set as test1, but it can be changed to test2, test3, or test4
    }
}

My challenge lies in applying the active class to the appropriate navigation element based on the value of activeSection. For instance, if activeSection == test1, the first navigation element should receive the active class, and similarly for the subsequent sections.

Answer №1

I find this to be quite straightforward

<nav class='navigation'>
 <div :class="{ active: currentSection==test1 }" @click='scrollTo(".test1&qout;)'> 
 </div>
 <div :class="{ active: currentSection==test2}" @click='scrollTo(".test2")'> 
</div>
<div :class="{ active: currentSection==test3}" @click='scrollTo(".test3")'> 
</div>
<div :class="{ active: currentSection==test4}" @click='scrollTo(".test4")'> 
</div>
</nav>

and remember to update the value of currentSection within the scrollTo method too https://v2.vuejs.org/v2/guide/class-and-style.html

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

Deciphering unidentified Json data

Having some trouble with an error in my note taker app built using expressjs. Everything was working fine until I tried to save a new note and it's throwing this error: SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () Here&apos ...

Javascript embedded within the application to transfer form data. Issue with routing functionality

Scenario In my rails app, I've integrated an embedded javascript form using Vue. External sites of shops can now paste and use this form to allow their visitors to search for available bike_categories. Objective After creating the form that can be co ...

Is there a way to automatically override the CSS cursor style?

Issue with SCSS styling on image link I attempted to modify the cursor style from pointer to default, but after saving and reloading my React app, the change did not take effect. I tried writing some code, but it seems that Stack Overflow is indicating an ...

Developing a RESTful API with Discord.js integrated into Express

Currently, I am faced with the challenge of integrating the Discord.js library into an Express RESTful API. The main issue at hand is how to effectively share the client instance between multiple controllers. The asynchronous initialization process complic ...

"Encountering an Invalid hook call error with React-Leaflet v4 and Next.js 13

I am facing an issue following my update of Next.js from version 12 to 13, which also involved updating React from 17 to 18 and react-leaflet from 3 to 4. Within this component: ` function ChangeView({ center }) { const map = useMap(); map.setView( ...

Submitting a form with Multer when the user chooses to upload a file or not

I have integrated multer into my express app for handling form submissions. The issue I am facing is with the optional image upload feature in the form. Users have the choice to add a photo if they want, but they should also be able to submit the form wi ...

Applying background-image in ngStyle results in an undefined value

I have been attempting to incorporate images (retrieved through an API) as the background-image of an element. However, I keep encountering the error message Cannot read property 'url' of undefined, even though the URL is actually being rendered. ...

How can I manually transclude content within a directive into two separate locations?

When trying to access the result of ng-repeat, I discovered that using the transclude function and manually compiling could help. However, this method does not work in situations with two places and elements containing ng-repeat. Here is how my code is str ...

Is there a way to automatically recalculate the "Total Price" when the input values are adjusted?

Whenever I add an item to the cart, it gets appended to the row in the shopping cart, and the price adjusts accordingly. However, I'm having trouble getting the price to adjust whenever I change the input values (input type: "number"). I can't se ...

I need to mass upload a collection of resumes stored in a zip file, then extract and display the content of each resume using a combination of HTML

I recently used a service to extract and retrieve the contents of a zip file. I am trying to read the content of the files and integrate them into the scope of my Angular project. Any suggestions would be greatly appreciated. Below is an outline of my func ...

encountering difficulty with loading JavaScript code while customizing form fields in Symfony/Sonata Admin

I have been attempting to personalize a field in a form created with the Sonata Admin Bundle. I followed the solution provided in this article: Customize form field rendering. Apart from customizing the form field, I also aimed to implement an autocomple ...

The object's type remains a mystery

While working on implementing jwt authentication in Ionic, React with TypeScript, I faced a typescript error when trying to add a check in my App.tsx file after successful implementation. The error stated: Object is of type 'unknown' Below is ...

What could be causing the inability to move down on this page?

Trying to scroll down a page using Selenium with Python and the execute_script command, but encountering issues. Despite executing the code provided below, I am unable to reach the bottom of the page: def create_browser(first_page=None): print "Starti ...

Attempting to implement a basic AngularJS integration with Google Maps for a straightforward demonstration

I have a basic Google Maps project that I am trying to transition into an AngularJS project. This is all new to me as I am a beginner in both AngularJS and web development so please bear with me :) The project can be found at https://github.com/eroth/angul ...

Implementing long polling for private messaging in PHP's chat functionality

Can you help me with a question I have regarding my chat form on the HTML page? Here is the code for the form: <form action="../addchat.php" method="POST" enctype="multipart/form-data"> <textarea id="textarea" style="border- ...

Use Angular2 to showcase the selected image as the main one when the user clicks on the

I'm working on creating a product thumbnail gallery, and I'd like the main image to be displayed when the user clicks on a thumbnail. I am using Angular for this project, although I am still learning my way around the framework. product.html &l ...

How to pass route parameters using v-link in Vue Router

Within the Parent.vue file, I have included this anchor tag: <a v-link="{ path: '/somepath/somesubpath', query: { messageId: 999}}"> Here </a> And also this one: <a v-link="{ path: '/somepath/somesubpath', params: { me ...

Retrieving environmental information within a Vue component

I am trying to display information from my .env file, specifically the APP_NAME, in my components. For example, I want to greet users with Welcome to {{APP_NAME}}. UPDATE After referring to this documentation, I have updated my env file as follows: MIX ...

Creating a distinct header value for every $http request

I've been assigned the task of adding a unique ID for each request to all HTTP requests made by our AngularJS application for logging purposes. While this is more crucial for API calls, I'm currently working on implementing it for all kinds of re ...

I'm currently attempting to establish a connection between my server.js express API and MongoDB, but I keep encountering an unfamiliar error message that I'm having trouble decipher

Here is the server.js code: import express from 'express'; import bodyParser from 'body-parser'; import userRoutes from './routes/users.js'; import mongoose from 'mongoose'; co ...