Ways to leverage the power of Reactivity APIs in Vue.js

In my Vue application, I am trying to create a Drop Down Menu by using a variable called "showDropDown". The idea is to use a v-if directive to check if the value of showDropDown is true, and then display the menu accordingly. Additionally, I want to implement a feature where clicking on the Menu icon toggles the visibility of the Menu by changing the value of showDropDown to false. However, I have encountered some issues with the code below. It seems to work fine when vue is used through CDN, but not in my current setup.

import HelloWorld from './components/HelloWorld.vue'
import TheWelcome from './components/TheWelcome.vue'
import MainPage from './components/MainPage.vue'
import ProfilePage from './components/ProfilePage.vue'
import FriendsPage from './components/FriendsPage.vue'
import SettingsPage from './components/SettingsPage.vue'
import SignOutPage from './components/SignOutPage.vue'

const currentView =  "mainPage";
const showDropDown = ref(false);
const imgSrcProfile = "./src/assets/defaultUserIcon.webp";
const imgSrcLogo = "./src/assets/DailyQuotes_Logo.png";

function toggleDropDown() {
  console.log("test");
  showDropDown = !showDropDown;
}
</script>

<template>
  <main>
    <header>
      <div class="logo">
          <img v-on:click="currentComponent='mainPage'" :src="imgSrcLogo">
      </div>
      <div class="profile" >
          <img v-on:click="toggleDropDown" :src="imgSrcProfile">
          <div id="dropDownMenu" v-if="showDropDown">
              <a v-on:click="currentComponent = 'profile'">Profile</a>
              <a v-on:click="currentComponent = 'friends'">Friends</a>
              <a v-on:click="currentComponent = 'settings'">Settings</a>
              <a v-on:click="currentComponent = 'signOut'">Sign out</a>
          </div>
      </div>
  </header>
    <MainPage/>
  </main>
</template>

Answer №1

Make sure to check out the Vue reactivity documentation for a better understanding of how ref functions. The ref object acts as a proxy, and you should always update the inner value property rather than trying to reassign the ref object itself.

Here is an example of how your code related to showDropDown should be structured:

<img :src="imgSrcProfile" @click="toggleDropDown" />
<div v-if="showDropDown" id="dropDownMenu">
  <a @click="currentComponent = 'profile'">Profile</a>
  <a @click="currentComponent = 'friends'">Friends</a>
  <a @click="currentComponent = 'settings'">Settings</a>
  <a @click="currentComponent = 'signOut'">Sign out</a>
</div>
import { ref } from 'vue'

const showDropDown = ref(false)

function toggleDropDown() {
  showDropDown.value = !showDropDown.value
}

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

What is the best way to pass only the second parameter to a function in TypeScript?

Let's consider a TypeScript function as shown below: openMultipleAddFormModal(commission?: Commission, people?: People): void { // some data } To make a parameter optional, I have added the Optional Chaining operator. Now, how can I modify the code ...

Restrict the number of dynamic form elements to a maximum of 10 entries

I am working on a feature where users can refer their friends and the data will be saved in a database. <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type='text/javascript' sr ...

What steps should I take to ensure a local HTML page retains the current section that is hidden open whenever it is reloaded?

One of the challenges I have encountered with my local HTML note-taking file is that, despite dividing it into hidden sections accessible by clicking on buttons at the top of the page, reloading the content resets it back to its default state. This means t ...

Enhancing Symfony's performance through optimized Ajax response time

When using Symfony2, I am experiencing differences in loading times for AJAX requests between development and production environments. In development, it takes 1 second to load, while in production it only takes 500 milliseconds for a simple call: Here is ...

What is the most effective method for identifying duplicate values in a multidimensional array using typescript or javascript?

I have a 2D array as shown below: array = [ [ 1, 1 ], [ 1, 2 ], [ 1, 1 ], [ 2, 3 ] ] I am looking to compare the values in the array indexes to check for duplicates. For example array[0] = [1,1]; array[1] = [1,2]; array[2] = [1,1]; We can see that ...

Converting Greek text to UTF-8 using Javascript

Currently, I am working on a project with my teacher to digitalize a Greek textbook by transforming it into an online application. This process involves the conversion of a Shapefile, which draws polygons on maps along with polygon descriptions, and mappin ...

Prevent anchor jumping caused by popstate event in Safari

This situation is really testing my patience. When navigating within the same page using anchors, the browser automatically takes you back/forward to the location of that link in your history when popstate is triggered. One might think that you could prev ...

Utilize an external JavaScript file function within an AngularJS controller

I have an external JavaScript file with additional functions that I am trying to call from an Angular controller. Here is an example of the external.js file: ... ... function fun() { ... ... } ... ... The controller in question is called acccountCon ...

What is the best way to display data in the User Interface when data is being received through the console in AngularJS?

I have created an HTML file and the corresponding controller logic for this page. I can see the data in the console, but it's not displaying on my UI. <div id="panelDemo14" class="panel panel-default" ng-controller="NoticeController"> < ...

What is the most effective method to prevent postback controls from activating before the page is fully loaded?

My website contains high-quality graphics, which may lead to slow download times for users with poor internet connections. As the browser is still loading, users often access form options and submit their information prematurely. This premature submission ...

Disabling eventListener upon the occurrence of another event is ineffective

I am having trouble with two functions in my app that create different HTML structures. I want to close the info button event when the menu_div button is clicked, and vice versa. Can anyone provide some help? const menu_div = document.createElement("butt ...

Generate a Vue component that automatically wraps text dynamically

Challenge I am looking for a way to dynamically wrap selected plain text in a Vue component using the mouseUp event. For instance: <div> Hello World! </div> => <div> <Greeting/> World! </div> Potential Solution Approach ...

Divide a string into separate numbers using JavaScript

This snippet of code is designed to search the table #operations for all instances of <td> elements with the dynamic class ".fuel "+ACID: let k = 0; let ac_fuel = 0; parsed.data.forEach(arrayWithinData => { let ACID = parsed.data[k][0]; ...

Is there a way to preserve EXIF data when converting an image to base64?

I am currently facing an issue with reading a local image that has been created with a different exif.Orientation based on the degree of rotation. const exifData = piexif.load(data.toString("binary")); // Assign the desired orientation value ...

Making an input field in Vue automatically read-only when a specific value is met

Is it possible to make an input field read-only in Vue.js based on data values? Here's a situation: <select class="form-control" id="selectCategory" :readonly="cat_id >= 1" name="cat_id"> I'm looking for a wa ...

Struggling to concentrate on a text field following navigation in an AngularJS application

My current project involves working with an AngularJS application where I am facing a challenge in setting the cursor or focus on a specific text box when routing to a page. Despite my attempts using various methods such as setFocus() in JavaScript, autofo ...

Switching Bootstrap Navbar Active State with JavaScript

I have encountered an issue with using the "active" class on my navbar navigation items in Bootstrap 4. When I click on the links, the active state does not switch as intended. I have tried incorporating JavaScript solutions from similar questions but have ...

Utilizing Express Routes to specify React page components

Currently, as I delve into the world of learning Express.js, I find myself faced with a unique scenario involving 2 specific URLs: /security/1 /security/2 As per the requirements of these URLs, the expected response will vary. If the URL is "/securi ...

Update the text for the filter search placeholder in the Ant Table component

Is there a way to alter the default placeholder text in the Ant Table? I've set up a functioning example in documentation but couldn't find any prop for customization besides the customized filter dropdown, which I didn't want to implement. ...

Using Vue in conjunction with TypeScript and CSS modules

I am facing an issue with my SFC (single file vue component) that utilizes TypeScript, render functions, and CSS modules. <script lang="ts"> import Vue from 'vue'; export default Vue.extend({ props: { mode: { type: String, ...