Is it possible in Vue.js to create a reactive functionality for a button using a watcher for "v-if" condition?

I have a userlist page with various profiles, including my own. A button is supposed to appear only when viewing my own profile.

The issue arises when switching from a different profile to my own (changing the router parameter) - the button should show up, but it doesn't work in reverse (my-profile -> another profile the button disappears).

Currently, the button only shows up when I switch from a different route (e.g., from the homepage to my profile).

On my Profile.vue

<v-btn
    v-if= "isUserProfile()"
    outline
    large
    fab
    v-on="on"
    color="indigo">
<v-icon>edit</v-icon>
</v-btn>

A watcher in Profile.vue checks for changes in the URL/router parameters.

watch: {
    '$route.params.username': {
        immediate: true,
        handler (value) {
            this.username = value
            this.getId()
            this.isUserProfile()
        }
    }
},

Using the method:

isUserProfile () {
    return (this.$store.state.route.params.username === this.$store.state.user.username)
},

The button should appear every time I switch to my own profile from another one since it works in the opposite scenario. However, there seems to be a problem as demonstrated in this example gif where my profile is "123123":

https://gyazo.com/cf47036bfb60cc6490c538e50a32db81

Manually changing the parameters in the URL by switching between profiles reveals the inconsistency of the button display. It should always be visible when switching back to my profile.

Answer №1

Well, it turns out that the solution is quite straightforward and the complexities of converting it into a computed value were unnecessary, as I just verified (although many would argue that using a computed value is a better practice).

Solution: Instead of using v-if:isUserProfile, consider using v-show:isUserProfile; this way, the button will change based on what needs to be achieved.

I am now going to delve into https://v2.vuejs.org/v2/guide/conditional.html for more insights.

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 modifications can be made to the code in order to show the upload progress for each individual file when uploading multiple files simultaneously?

I am currently working on an uploader that can handle multiple file uploads and displays the progress of each uploaded file. However, I am facing some difficulties in showing the progress of every individual file. Can anyone provide me with assistance? &l ...

What are some strategies for implementing dynamic script generation within an AJAX response?

I am exploring a new AJAX design approach where a script is returned to handle and show data. The JSON response below is currently functional, but I would appreciate advice on how to better organize the application for future maintenance. { payload: " ...

Is there a method to rename the _nuxt folder?

Hello, I am having trouble with a Nuxt.js application that I can't seem to figure out. What I am trying to do is change the name of the generated _nuxt folder to something else. I have updated the nuxt.config.js file and added this snippet: build: { ...

Verify whether an option is chosen in CakePHP 3 form

I'm working on a form where users can select Types and each type has an associated color. In my TypesController, I have a function that retrieves the list of types with their IDs, names, and colors. $types = $this->Types->find('list') ...

Pagination in DynamoDB: Moving forward and backward through your data

Currently, I am utilizing DynamoDB in combination with NodeJS to display a list of objects on the user interface. Given that DynamoDB can only process 1MB of data at a time, I have opted to implement pagination. This allows users to navigate between pages ...

Adding information to an HTML table using JQuery

Is there a way to incorporate JSON data into an HTML table? The JSON data follows this format: https://i.stack.imgur.com/lzdZg.png This is the desired HTML table structure: <table class="table table-bordered table-hover "> ...

What is the best way to establish my permit requirements on a monthly basis?

An employee is only allowed to request up to 3 permits per month. If they need a fourth permit, they will have to wait until the following month. Here is my table structure for permissions: Permissions id (integer), format (text), date_r ...

Utilizing Google App Engine for seamless deployment, integrating Ajax for dynamic interactions, and

Using the google App Engine, I am looking to implement javascript (or Ajax) for POSTing a form and updating the target div. The form includes multiple fields and files for transfer. The javascript function I am using is extracted from the "Javascript: The ...

How to extract various arrays of identical objects from a larger array in JavaScript

I am working with an array containing objects of this structure: {id: 543, firstName: 'Ted', lastName: 'Foo', age: 32} My goal is to filter out objects in the array that have the same values for both the firstName and age properties. ...

Incorporate the key as a prop within a Child Component in a React application

I am trying to display a list of elements in React, where the key of each element is used as an index in front of the item. However, when I try to access props.key, it just returns undefined. Does anyone have any suggestions on how to access the key proper ...

Is it possible to run javascript code within a Vue component's template?

I'm facing an issue in a vue.js component where I am attempting to compile a variable in a template, but it is not being compiled or converted correctly. Can anyone provide guidance on how I can achieve the desired outcome with the following code: &l ...

Is there a potential impact on performance when utilizing local variables instead of repeatedly accessing properties?

Examining JavaScript code optimized for a performance-sensitive environment, specifically a game engine in mobile settings. Oftentimes, this code avoids using local variables and instead relies on explicit chains, such as: if (this.x.y[z].i) { this.x ...

Manipulating toggle buttons using CSS and jQuery

Check out this jsfiddle to see my attempt at creating a toggle switch in the form of a mute button. The toggle button shows the current setting: mute or unmute When hovered over, it displays the alternative setting However, I am encountering an issue wh ...

The error message "system.js:4 Uncaught (in promise) error" popped up unexpectedly as I was working on my ES

I'm dipping my toes into ES6 development using Plunker. I've loaded Traceur and SystemJS, but I'm encountering the following errors: GET https://traceur-compiler.googlecode.com/git/bin/traceur.js 404 () VM490 system.js:4 GET https://run.pln ...

Using VueJS to pass objects as props

My current project involves building controllers for sets of items. I am creating a Javascript object with functions and storing it in the data of the Vue instance. This object is then passed using: <dynamic-table :table-object="objTable"></dynami ...

MongoDB Stitch retrieves all data fields

Can anyone help me with my MongoDB query issue? I've recently started working with mongoDB and I'm having trouble getting just one field back for all my documents. var docs = db.collection("articles").find({}, { _id: 0, title:1}).asArray(); De ...

What measures can be taken to ensure that the input checkbox is not toggled by accidentally pressing

There's a checkbox input in a dropdown menu at the top of the page that is giving me some trouble. When I select an option by clicking on it, for some reason pressing the spacebar toggles it off and on. Clicking outside once doesn't correct it, I ...

Error occurs when attempting to filter data through input text pasting in Angular

Currently, I am working on a web application that utilizes the Angular framework and angularfire2. The issue I am encountering is related to the data filter functionality not functioning correctly when pasting copied text. Interestingly, it works perfectly ...

`npm run build` fails to create the `index.html` file

Upon executing the command npm run build in my Vue.JS project, I noticed that there is no index.html file in the dist directory. The contents of my webpack.base.config.js file are as follows: // Your webpack base config code here // ..................... ...

TypeScript and the Safety of Curried Functions

What is the safest way to type curried functions in typescript? Especially when working with the following example interface Prop { <T, K extends keyof T>(name: K, object: T): T[K]; <K>(name: K): <T>(object: T) => /* ?? */; ...