繁体   English   中英

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

[英]How to get variable value from other js files in nuxt js

我有一个nuxt js项目,并创建项目文件目录,如下所示:

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

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; 

一些网页组件从异步请求中获取结果,而我想使用另一个js文件(或第三方库)中的结果。 异步请求的结果保存在哪里,无论路由如何更改,如何要求结果?

首先,您必须导出该变量:

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

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

// ... (all else the same)

并在需要时导入其他地方:

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