简体   繁体   中英

How to get variable value from other js files in nuxt js

I have a nuxt js project, and make project file directories like below:

| -- axiosUtility
       | -- index.js
| -- pages
| -- store
| -- other directories

The file of axiosUtility/index.js is below:

 /* 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; 

Some web page component get a result from asynchronous request, and I want use the result from another js file(or third party library). Where does I save the result of asynchronous request, and how can I require the result no matter of the changes of route?

First, you must export that variable:

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

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

// ... (all else the same)

And import it elsewhere, whenever you need it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM