I am looking to identify which tabs are active based on the current day using vuejs

With 7 tabs on my Vue.js page, I aim to activate the tab corresponding to the current day. However, I am facing a challenge where only the active tab is being highlighted and not allowing me to navigate to others after applying if-else statements. How can I ensure that all tabs are activated upon click and not just based on the current day?

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

<template>

  <div>
    <div id="tabs" class="container">
      <div class="tabs">
        <a v-on:click="timestamp = 1"
           v-bind:class="[timestamp === 1 ? 'active' : '' ]">Friday</a>
          <a
            v-on:click="timestamp = 2"
            v-bind:class="[timestamp === 2 ? 'active' : '']"
            >Thursday</a>
          <a
            v-on:click="timestamp = 3"
            v-bind:class="[timestamp === 3 ? 'active' : '']"
            >Wednesday</a>
          <a
            v-on:click="timestamp = 4"
            v-bind:class="[timestamp === 4 ? 'active' : '']"
            >Tuesday</a>
          <a
            v-on:click="timestamp = 5"
            v-bind:class="[timestamp === 5 ? 'active' : '']"
            >Monday</a>
          <a
            v-on:click="timestamp = 6"
            v-bind:class="[timestamp === 6 ? 'active' : '']"
            >Sunday</a>
          <a
            v-on:click="timestamp = 7"
            v-bind:onClick="[timestamp === 7 ? 'active' : '']"
            >Saturday</a>
        </div>

        <div class="content">
          <div v-if="timestamp === 1 " class="tabcontent">
        </div>

        <div v-if="timestamp === 2" class="tabcontent">
          Content for tab two
        </div>
        <div v-if="timestamp === 3" class="tabcontent">
          Content for tab three
        </div>
        <div v-if="timestamp === 4" class="tabcontent">
          Content for tab three
        </div>
        <div v-if="timestamp === 5" class="tabcontent">
          Content for tab three
        </div>
        <div v-if="timestamp === 6" class="tabcontent">
          Content for tab three
        </div>
        <div v-if="timestamp === 7" class="tabcontent">
          Content for tab three
        </div>

      </div>
    </div>

</template>

<script>

  import moment from 'moment';

  export default {
    data: function() {

         return {
           activetab:'',
           pageName: 'MB',
           pageDescription: 'This is MB',

        data: {
        timestamp : '',
         activetab:''
       },

   }
    },
    
    
    name: 'mb',
    
    computed: {
      timestamp : function() {
        if ( moment().format('dddd') === "Saturday"){
          return 7
         } else if ( moment().format('dddd') === "Sunday") {
          return 6
        }else if ( moment().format('dddd') === "Monday") {
          return 5
        }else if ( moment().format('dddd') === "Tuesday") {
          return 4
        }else if ( moment().format('dddd') === "Wednesday") {
          return 3
        }else if ( moment().format('dddd') === "Thursday") {
         return 2
       }else if ( moment().format('dddd') === "Friday"){
         return 1

      } else (moment().format('dddd') === "hassan")
       return 2
    }
   },
  };

</script>

Answer №1

To resolve the issue of assigning a value to a computed property without a setter, one approach is to include the initial logic within the created hook and assign the value to a variable named timestamp.

(Additionally, utilizing a switch statement could prove beneficial in handling multiple if conditions)

<script>
import moment from 'moment';
export default {
  data: function() {
    return {
       activetab:'',
       pageName: 'MB',
       pageDescription: 'This is MB',
       timestamp: 1,
       data: {
         timestamp : '',
         activetab:''
       }
     }
  },
  name: 'mb',
  created() {
    if ( moment().format('dddd') === "Saturday"){
      this.timestamp = 7
    } else if ( moment().format('dddd') === "Sunday") {
      this.timestamp = 6
    } else if ( moment().format('dddd') === "Monday") {
      this.timestamp = 5
    } else if ( moment().format('dddd') === "Tuesday") {
      this.timestamp = 4
    } else if ( moment().format('dddd') === "Wednesday") {
      this.timestamp = 3
    } else if ( moment().format('dddd') === "Thursday") {
      this.timestamp = 2
    } else if ( moment().format('dddd') === "Friday"){
      this.timestamp = 1
    } else (moment().format('dddd') === "hassan")
      this.timestamp = 2
  }
}
</script>

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

Attempting to update state on a component that is no longer mounted

There are many instances in my components where I find myself needing to execute the following code: function handleFormSubmit() { this.setState({loading: true}) someAsyncFunction() .then(() => { return this.props.onSuccess() }) . ...

When I use `console.log` with `req.body` in Node, the password is

Currently, I am utilizing NodeJs on the backend to console.log(req.body) data from a form that gathers usernames and passwords from users. I have noticed that this method exposes the collected username and password information. Should I be concerned abou ...

Utilize an array value as a parameter for getStaticProps within Next.js

Currently, I am fetching my Youtube playlist using a fetch request and utilizing getStaticProps(). However, I am encountering an issue where my playlist is dependent on the result of an array of objects. export async function getStaticProps(){ const MY_P ...

The validationSchema function argument value in vee-validate@next is consistently observed to be undefined

I recently created a simple login form in Vue 3 and I'm currently attempting to validate it using the vee-validate@next composition API method. You can view the code on stackblitz: https://stackblitz.com/edit/vue-zauhqb However, I've encountered ...

The instance is referencing "firstName" during render, but it has not been defined as a property or method

I am encountering a persistent warning message related to the inputs on my Signup.vue page. This warning is appearing for all four input fields in my code: firstName lastName email password The warning mentioning specific instances keeps popping up consi ...

Seeking assistance in incorporating items from one array into another array

I have a task involving two arrays - one named a, containing 5 values, and another named s which is currently empty. The objective is to identify the smallest value in array a, add it to array s, and then remove it from array a. Below is the code I have im ...

Updating Vue component properties with data obtained from an external source

Vue.component('unique', { template: `some custom html`, data() { { return { uniquedata: 'hello, beginning!' } } }, methods: { retrieveData: function fetchData() { fetch('http://localhos ...

Server experiencing slow performance with Node.js formidable file uploads

When sending files as form data along with some fields using an http post request in angular.js and receiving file in app.post in node.js, the performance is inconsistent. While local testing shows a fast upload speed of 500 mb/sec with formidable, on the ...

Issue with series of node commands getting stuck on npx command

I have a custom node script that I use to automate the setup of my application. This script creates directories, generates files, and executes a series of node commands. Everything functions correctly for the most part. The specific commands being executed ...

Discrepancy between Laravel Vue: console logging Axios POST response and network response apparent

https://i.stack.imgur.com/vO05x.png Within my Laravel vue app's backend, I am noticing an inconsistency in the data being sent and received. Even though the data is supposed to be the same, the two console logs are showing slight differences. DATA ...

Node.js Express Issue: Module Not Found

Having trouble launching an express app in docker with node 10.9.0 due to an import problem: root@e85495ae1c9e:/usr/app/backend# node app.js internal/modules/cjs/loader.js:583 throw err; ^ Error: Cannot find module '/usr/app/backend/models/todo&ap ...

Firefox returns a "null" error when attempting to access the Vimeo API, whereas there are no errors reported when accessing the YouTube API or when

I tried researching this issue on multiple platforms, including Google and other forums, but I am still unable to resolve it. I recently transitioned from a proxy request approach (using AJAX to communicate with a server-side script that then interacts wit ...

The function persists in outputting a true result, despite the fact that it is expected to output

Currently, I am working on a NextJS project where I have a client-side form. I've been attempting to implement validation for the form by creating a separate function called validateForm(). However, no matter what input is provided, the function alway ...

Increase the worth of current value

Whenever a user enters their name into an input box, I want it to be shown after the word 'hello'. However, currently, the word 'hello' gets replaced by the user's name instead of being displayed after it. var name = document.ge ...

Ways to add the class "active" to the initial element in a Vue.js for loop

I am currently diving into learning vue.js and am working on adding a list of elements. My goal is to apply the class="active" only to the first element in the for loop. Below is the snippet of my code: <div class="carousel-inner text-center " role="li ...

Experiencing a problem with inline JavaScript onclick

I recently came across a fantastic pure javascript code for a smooth scrolling function, but I'm struggling with integrating it into an inline onclick function. My understanding of javascript is quite limited and I could really use some help... <d ...

Show targeted information from the array using the respective identifier

Is it feasible to exhibit data from a Vuex store array in a unique manner, comparable to the illustration provided below: <template> <div> <h1>{{this.$store.state.data.title}}</h1> <p>{{this.$store.state.da ...

How can I position a div in the center of a fullpage.js slide?

I'm currently utilizing the fullPage.js plugin sourced from this link: . I have successfully implemented vertical slides by using the following HTML code: <div class="section" id="section2"> <div class="slide" id="slide1"> ...

Tips on creating reusable scoped styles for 3 specific pages in Vue.js without impacting other pages

Our project includes global classes for components that are utilized throughout the entire system. <style lang="scss" scoped> .specialPages { padding: 0 10px 30px 10px; } /deep/ .SelectorLabel { white-space: nowrap; al ...

What is the best way to access a component's value from a different component in Vue script?

I have two Vue components in my PHP file: "application" and "vn" component. I am trying to fetch {{obj.vacancies_left}} from the "vn" component and use it in the "application" component. However, I keep getting an undefined variable error. I attempted to r ...