Tips for transferring information between .vue files

After successfully logging in with the Prime Vue template, I have stored the user data in an object variable. Now, I need to access this user data on the profile page without importing a component. Can someone guide me on how to achieve this?

Below are the code snippets from my profile page:

<script setup>
import { computed, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useLayout } from '@/layout/composables/layout';
import axios from 'axios';
let user = ref({});
const router = useRouter();
const rememberMe = ref(false);
const { layoutConfig } = useLayout();
const e_mail = ref("");
const pass = ref("");
const userData = computed(() => {
    return user;
});
</script>

<template>
    <div class="px-5 min-h-screen flex justify-content-center align-items-center">
        <div class="border-1 surface-border surface-card border-round py-7 px-4 md:px-7 z-1">
            <label for="">Username</label>
            <span></span>
            <label for="">E-Mail</label>
            <span></span>
            <label for="">Phone</label>
            <span></span>
        </div>
    </div>
</template>

And these are the codes from my Login Page where I tried using emit function but encountered issues:

<script setup>
import { computed, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useLayout } from '@/layout/composables/layout';
import axios from 'axios';
let user = ref({});
const router = useRouter();
const rememberMe = ref(false);
const { layoutConfig } = useLayout();
const e_mail = ref("");
const pass = ref("");
const navigateToDashboard = async () => {
    axios.post("http://localhost:3000/api/user/login", {email: e_mail.value, password: pass.value})
    .then(response =>  {
        user = response.data.user;
        console.log(user);
        emit('userData', user);
        router.push({ name: 'e-commerce' });
    });
};
const userData = computed(() => {
    return user;
});
</script>

<template>
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1600 800" class="fixed left-0 top-0 min-h-screen min-w-screen" preserveAspectRatio="none">
        <rect :fill="darkMode ? 'var(--primary-900)' : 'var(--primary-500)'" width="1600" height="800" />
        <path
            :fill="darkMode ? 'var(--primary-800)' : 'var(--primary-400)'"
            d="M478.4 581c3.2 0.8 6.4 1.7 9.5 2.5 ...
        </path>
        ...
    </svg>
    <div class="px-5 min-h-screen flex justify-content-center align-items-center">
        <div class="border-1 surface-border surface-card border-round py-7 px-4 md:px-7 z-1">
            <div class="mb-4">
                <div class="text-900 text-xl font-bold mb-2">Log in</div>
                <span class="text-600 font-medium">Please enter your details</span>
            </div>
            ...
        </div>
    </div>
</template>

If anyone has suggestions on passing data between pages without importing components, please share. Thank you.

Answer №1

It is important to centralize data shared among multiple components, like user authentication details and website settings, in vuex or pinia for easy access in various parts of the application such as the sidebar, navbar, user profile, and website settings.

Duplicating this shared data across all components would result in poor code quality.

You can also consider storing certain data in localStorage, such as tokens, to persist information between page reloads.

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

When initializing a new Date object with a number versus a string, the resulting values will vary

As I delve deeper into JavaScript, I stumbled upon an interesting quirk in its behavior. When creating date objects like this: var stack = new Date(1404187200000) // 07-01-2014 var overflow = new Date('07-01-2014') I noticed that when comparin ...

Discover the power of catching Custom DOM Events in Angular

When working with an Angular library, I encountered a situation where a component within the library dispatches CustomEvents using code like the following: const domEvent = new CustomEvent('unselect', { bubbles: true }); this.elementRef.nati ...

Instructions for invoking a function that has been passed to a Vuex action function is as follows

I am facing an issue with a button located inside my Vue component <button class="search-participant__btn" @click="getParticipant({iin, index, switchIsLoading})">Search</button> The getParticipant function is part of the ac ...

Formatting Objects and Arrays in React with JavaScript

I previously had access to the first item in my local data using data{0}. However, upon transitioning to a database environment and retrieving the data to create another object, I find myself unable to access the first item using data{0}. It seems like th ...

Maintain the state of the previous page in Vue to ensure continuity

Currently in the process of creating a small PWA to simulate an Android app, I have discovered Vuejs. However, I encountered an issue that has proven difficult to resolve. While scrolling through lists on the homepage for movies, TV shows, or news, clicki ...

Angular.js is throwing an error at line 13708, stating: "Error: [ng:areq] Function 'homeController' is missing or undefined."

Encountered an issue while trying to reach HomeController (module controller). Upon clicking the "home" link, an error appeared in the console stating that "HomeController is not a function; undefined. Could you advise on where I should register this cont ...

Show the child div using JavaScript when hovering over it

I am currently working with the following HTML structure: <div id="wrapper"> <div onmouseover="displayDiv()"> <div id="thisIsTheDivToDisplay"></div </div> <div onmouseover="displayDiv()"> <div id="thisIsT ...

Guide to creating a nested list using the jQuery :header selector

Here is the structure of my html: <h1 id="header1">H1</h1> <p>Lorem ipsum</p> <h2 id="header2">H2</h2> <p>lorem ipsum</p> <h2 id="header3">H2</h2> <p>lorem upsum</p ...

Transferring PHP objects to JavaScript arrays

I have been searching for a unique JS array of PHP arrays with a specific format. After hours of research and attempts to use the json_encode PHP function, I still haven't achieved the desired format. Here's an example of what I'm looking fo ...

Preserve aspect ratio when applying a texture map to a threejs object

I have a project idea to develop a 3D shoe design tool that allows users to create patterns and apply them to the shoes. I am facing an issue with adding an image to a Three.js material. The texture appears blurry after updating it. As a beginner in Three ...

Guide on implementing the recently established attribute ID using jQuery

In my code snippet, I am assigning a new id to a button. Here is the code: $("#update").click(function(){ $("#update").attr('id', 'cancel'); }); After changing the id, I want to make it functional again in jQuery by reverting it b ...

Change to JSONArray using angularjs

Here is a json object: "values": [ {"name": "name1"}, {"name": "name2"}, {"name": "name3"} ] I want to convert it into this format: values: ["name1", "name2", "name3"]; Can this conversion be done in AngularJS or any other JavaScript functi ...

What is the best way to save an object with methods in a Vue application?

Looking for the best way to store complex objects with methods in Vue? Take this object as an example: const behavior = { onClick() { console.log('click') }, onDoubleClick() { console.log('double click'); }, on ...

CodePen experiencing issues with JS/jQuery coding functionality

Experimenting on CodePen, I am practicing using JS and jQuery with HTML. HTML: <button>asdasads</button> JS: $('button').on('click', function() { alert('woot'); }); Visit this pen, unfortunately it's ...

Encrypting href links in Nodemailer with Handlebars for forwarding purposes

I am working on a project that involves utilizing NodeMailer + Handlebars for sending and tracking emails. I am interested in changing every href link to the project URL before redirecting to the destination link. For example: Original email: <a href ...

When passing parameters through a URL in TypeScript, the display shows up as "[object object]" rather than as a string

Hey there! I'm trying to pass some string parameters to my URL to fetch information from an API. Everything seems fine, and when displayed in an alert, the URL looks exactly as it should (no [object, object] issue). var startDate = "2020-09-20"; var ...

React Select is failing to display the selected value

I'm currently learning react-redux and encountering a couple of issues. In my code, I am using React Select Plus in the following manner: <Select id="portf" options={opts} onChange={value => portfolioSelector(value ...

What are the best ways to optimize and capitalize on functionality in Electron.js?

After creating three custom buttons for the close, maximize, and minimize functions in Electron.js, I encountered an issue. While the close button is functioning properly, I am struggling with implementing the maximize and minimize buttons. In fact, I have ...

Try utilizing the Array.map method with a two-dimensional array

My current challenge involves a 2-dimensional Array where I am attempting to implement a "randomBool" function on each of the elements within it. The "randomBool" function essentially generates a random boolean value: const randomBool = () => Boolean( ...

The input value remains a string even after it has been converted to a float

I can't figure out where I'm going wrong. I'm attempting a simple task: getting user input, converting it to a float, and performing a calculation with that value. However, all I keep getting is NaN. Here's the input (I normally replac ...