Combining v-on:click and v-link in Vue.js

I'm currently working on a Vue.js application and I am in the process of creating a login system that involves multiple components.

Within my App.vue component, which serves as the main component with the navigation bar, there is a button that looks like this:

<a class="nav-item" v-link="'login'" v-if="!user.authenticated">Login</a>

...

import auth from '../auth'

export default {
 data(){
  return{
   user = auth.user
  } 
 }
}

In another file (/auth/index.js), I define the "auth" methods in the following manner:

...
  user: {
    authenticated: false
  },

  login(email, password) {
    axios.post(LOGIN_URL, {
      email,
      password
    }).then((response) => {
      this.user.authenticated = true;
      this.user = response.data;
      router.go('/home');
    })
    .catch((error)=>{
      console.log(error);
    });
  }

Additionally, I have created another component called Login.vue that handles the login template view and invokes the "auth.login" method with the necessary parameters.

My goal is to update the user value in App.vue with the information from auth.user after the user successfully logs in. What would be the best approach for achieving this? Should I prioritize managing v-on:click and v-link or create a new method?

EDIT:

I've been attempting to use the beforeRouteEnter method but so far, I haven't found success.

Answer №1

After some research, I came across a helpful solution that involved the following snippet of code:

watch: {
  $route () {
    this.user = auth.user
  }
},

I found guidance from the documentation provided here

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

Tracking triggered JavaScript events (across all browsers)

Although this query may have been addressed previously, the responses mostly pertain to browser-specific techniques. My inquiry is straightforward: Is there a method to observe all triggered events (specifically the fired event and the corresponding elemen ...

In Javascript, you can enhance your axes on a graph by adding labels at both the

Is there a way to add labels at the beginning and end of the axes to indicate the importance level, such as "not very important" and "very important"? I am currently utilizing a slider program from here. Since I am new to JavaScript, I would greatly appre ...

Separating a variable within a Twitch bot: techniques and tips

I am working on setting up a feature in my Twitch bot where it can respond to the command !test [var]. For example, if someone types !test @jeff, the bot would reply with hello @jeff. Currently, I am using tmi. client.on('chat', function(channe ...

Display the current date in YYYY/MM/DD format using a single method in React and TypeScript

Is there a better way to retrieve YYYY/MM/DD data using just one method? I attempted the following: date = created_at // from API const sendDate = `${String((date.getMonth() + 1)).padStart(2, '0')}${String(date.getDate()).padStart(2, '0&apos ...

Generating new objects from API request in React and aggregating them into a single, comprehensive object

I have developed a program that utilizes Axios to fetch data through API calls. I aim to save the fetched result as an object within my this.state.matrixDictionary variable. However, each time I make another API call, the previous object gets replaced. My ...

Using React links in combination with dangerouslySetInnerHTML to render content is a powerful and flexible technique

I am in the process of creating a blog page for my react application. The content on this page is retrieved from a CMS and contains raw HTML code that I display by using: <div dangerouslySetInnerHTML={{__html: this.state.content}} /> However, when I ...

The logs of both the frontend and backend display an array of numbers, but surprisingly, this data is not stored in the database

I am attempting to recreate the Backup Codes feature of Google by generating four random 8-digit numbers. for(let i = 0; i < 4; i++) { let backendCode = Math.floor(Math.random() * (99999999 - 10000000 + 1) + 10000000); backendCodes.push(back ...

Update dynamically generated CSS automatically

Is there a way to dynamically change the CSS? The problem I'm facing is that the CSS is generated by the framework itself, making it impossible for me to declare or modify it. Here's the scenario at runtime: https://i.sstatic.net/IovGr.png I a ...

Retrieving the value from a vuetify v-text-field to use in another text field

Looking to suggest a username based on the user's first and last name. The concept is that after the user fills out the first name and last name fields, the value in the username field will be a combination of their first and last names. firstName = B ...

Issues encountered when setting up a Context Provider in React using TypeScript

I am currently in the process of setting up a Cart context in my React TypeScript project, inspired by the implementation found here: https://github.com/AlexSegen/react-shopping-cart/blob/master/src/contexts/CartContext.js. I'm encountering some conf ...

Collaborative JavaScript repository within the Websphere Liberty platform

Is it possible to utilize a JavaScript library (such as Dojo, JQuery, or other custom developed libraries) as shared libraries within a Websphere Liberty server? For instance, I am interested in storing the .js files in either C:\wlp\usr\sh ...

Tips for creating list and detail pages using content retrieved from an API

I've encountered an issue with my nuxt project. When I try to route the page using nuxt-link, the component is not rendering on my page. I suspect that it's not making a fetch call. However, when I use a normal a href link, everything works per ...

One way to filter using a single array

Currently testing some angularJS with this particular example http://www.w3schools.com/angular/tryit.asp?filename=try_ng_filters_filter <ul> <li ng-repeat="x in names | filter : 'i'"> {{ x }} </li> </ul> Is ther ...

Struggling with the compilation of this Typescript code

Encountering a compile error: error TS2339: Property 'waitForElementVisible' does not exist on type 'signinPage' SigninPage code snippet: export class signinPage{ constructor(){ emailInput: { selector: 'input[type ...

Properties around the globe with Express & Handlebars

Currently, I am utilizing Handlebars (specifically express3-handlebars) for templates and Passport for authentication in my NodeJS application. Everything is functioning smoothly, but I have been contemplating if there is a method to globally pass the req. ...

Ways to Retrieve the Text From the Selected Index in Datalist Element

I need to extract the inner text of the option tag using pure JavaScript This is the HTML code I am working with: <input list="in" name="i_n" class="form-control" placeholder="Enter Item Name" required> <datalist id="in" onChange="rate(this)"&g ...

Console not displaying any logs following the occurrence of an onClick event

One interesting feature I have on my website is an HTML div that functions as a star rating system. Currently, I am experimenting with some JavaScript code to test its functionality. My goal is for the console to log 'hello' whenever I click on ...

Ways to limit Javascript math results to two decimal points and erase previous output in one go

Working on a JavaScript multiplication task. The input provided is multiplied by 0.05. The JavaScript code successfully multiplies the input number by 0.05, but encounters some issues: The calculated value should be rounded to two decimal points. For ex ...

What is the process for filtering records by a date greater than in the Material Table?

How can I filter the Material table by date values greater than the current value? I've been able to filter by exact date so far, but I need to filter all values that are greater than or equal to the current value in the table. <TableMaterial ...

No data being fetched through Ajax

I am facing an issue with retrieving PHP data in the form of an array. I have created an AJAX query to display data in two divs: one is a drop-down and the second is where all data will be shown. However, the problem is that when I attempt to do this, all ...