简体   繁体   English

如何从nuxt js中的其他js文件获取变量值

[英]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: 我有一个nuxt js项目,并创建项目文件目录,如下所示:

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

The file of axiosUtility/index.js is below: axiosUtility / index.js的文件如下:

 /* 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). 一些网页组件从异步请求中获取结果,而我想使用另一个js文件(或第三方库)中的结果。 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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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