Here’s a guide on accessing variable values from different JavaScript files in Nuxt.js

In my Nuxt.js project, I organized the file directories as follows:

/* Create a axios instance with custom headers */
import axios from 'axios';

let myVariable = someVariable //someVariable is the result from    // asynchronous 
//request with axios in some web page component, how can I get 
// variable?
// with vuex?

const myAxios = axios.create({
     headers: {'X-My-Variable': myVariable}
});

export default myAxios;

A web page component fetches a result from an asynchronous request, and I want to use this result in another JavaScript file or third-party library. Where should I store the result of the asynchronous request, and how can I access it regardless of changes in the route?

Answer №1

To start off, make sure to export the variable like so:

/* Creating an axios instance with customized headers */
import axios from 'axios';

export let myVariable = someVariable
//^^^^ -- included

// ... (everything else remains unchanged)

Then, you can easily import it in any other part of your code whenever necessary:

// importing from a file located at pages/foo.js
import { myVariable } from '../axiosUtility';

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

Is there a way to send both a List and an Integer from C# to Javascript?

Hello, I am looking for a way to pass List<ClassName> and count(int) values from my C# code-behind to JavaScript. Can anyone please guide me on how to achieve this? ...

Error: No targets with the name "taskname" were found for the custom Grunt task

Just recently, I decided to create my own custom task and here's the process: Started by making an empty folder named "custom-tasks" in the Document Root Next, I created the actual task file: "mytask.js" Implemented the functionality required for th ...

Is there a way to assign a preset value to an HTMX prompt?

Is it possible to specify a default value when the window.prompt is displayed? <a href="#" hx-prompt="Type your age" hx-get="/set/age">My Age: ?</a> Currently, the window.prompt appears blank. Is there a way to se ...

Forward to a different page after sending an ajax request

Hello, I am currently utilizing an ajax script that redirects to a PHP page for validation. Once all validation checks are successfully passed, I am trying to redirect to another page but encountering difficulties in implementing this functionality. Below ...

Origin Access Control - Failing to Function

I've been working with a PHP API that authenticates credentials. In the beginning of my PHP script, I include these lines: header('Access-Control-Allow-Origin: http://example.org'); header('Access-Control-Max-Age: 3628800'); heade ...

Encountering issues when implementing react-router with the client-side library

After following the author's suggestion, I've successfully loaded the client-side library. The recommended method is to simply drop a <script> tag in your page and use the UMD/global build hosted on cdnjs. I have ensured that ReactRouter i ...

Uploading multiple images using Cordova's File Transfer feature

Working on a project using Cordova and jQuery Mobile, my goal is to upload multiple images with the file transfer plugin. After successfully uploading one image, I am now attempting to upload 2 or 3 images in succession. Below is the HTML code snippet: ...

Ensuring the child div's height does not overflow beyond the parent div

When dealing with a parent div and a child div, both having different heights, how can I specifically retrieve the height of the portion of the child div that is within the boundaries of the parent div? Using document.getElementById("id").style.height prov ...

Invoke a function from a different vue.js component using the this.$refs property

I'm facing an issue with triggering a sibling element. What I've tried so far <b-img @click="callFileLoader"/> <b-file type="file" ref="fileUploader"></b-file> ... methods:{ callFileLoader () { this.$refs.fileUploader.c ...

Aligning text using Javascript and dynamically resizing it with css3 viewport causes a choppy effect when adjusting the size

I'm encountering some issues with keeping my text centered and smoothly resizing when the window size changes. I have a weather website that showcases only the temperature. I present the temperature in various ways. I utilize local storage to store t ...

Encountered an issue while executing the command (domain=com.apple.CoreSimulator.SimError, code=403):

Upon pressing play in XCode, I am encountering a specific error: An error was encountered processing the command (domain=com.apple.CoreSimulator.SimError, code=403): Incompatible device I have already attempted to resolve this issue by following a solutio ...

The content is spilling over onto the navigation bar

I am facing an issue with my blog site that I created using Django. The problem arose after adding a navigation bar, as the content of the website is overlapping the navigation bar when I scroll down the page. If you have any suggestions on how to add a ...

A guide on invoking an asynchronous function within the useEffect() hook in React

Is there a way to call an async function and get the result in my useEffect hook? I've come across examples using the fetch api directly inside the useEffect function. However, if the URL changes, all fetch calls need to be updated. When I attempted ...

Is there a way to ensure that the div of my template spans 100% in width and height?

I'm attempting to make my div 100% in size but I am having trouble achieving it. Here is my code: <template> <div> <Navbar/> <Calendar/> </div> </template> <script setup> import Calendar from &apos ...

Show information in a table based on a unique identifier

I am working with some data that looks like this [ { date: '20 Apr', maths: [70, 80.5, 100], science: [25, 20.1, 30] }, { date: '21 Apr', maths: [64, 76, 80], science: [21, 25, 27] }, ]; My goal is to present ...

Guide to correctly passing custom parameters along with the event object to an asynchronous form submission handler

Asking for guidance on defining and typing custom parameters alongside the native event object in an async onSubmitHandler for a form. The current implementation only receives the native event as a single parameter: const onSubmitHandler: FormEventHa ...

Using React Router (version 4) - navigating backwards in your application

Currently struggling to find a way to navigate back to the previous page using [react-router-v4][1] Here is the setup I have on my initial landing page: <Router> <div> <Link to="/"><div className="routerStyle"><Glyphicon ...

What is the best way to establish a default selection in Angular?

After retrieving JSON data from the server, it looks something like this: $scope.StateList = {"States": [ { "Id": 1, "Code": "AL", "Name": "Alabama" }, { "Id": 2, "Code": "AK", "Name": "Alask ...

A server-side rendered page in Next.js that functions without the need

Hey everyone, I'm curious about the best way to serve an HTML page in a Next Js application without relying on additional JavaScript. I need this because I want to make sure my webpage can be accessed by users who have older phones like Symbian or oth ...

having difficulty transmitting parameter to angular directive

After assigning a collection to a source variable, I am trying to activate a third party control (bootstrap-select) using a directive that watches the assigned collection. angular .module('app').directive('bootstrapDropdown', ['$t ...