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

时间:2018-04-11 12:37:02

标签: javascript vue.js axios vuex 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文件(或第三方库)的结果。我在哪里保存异步请求的结果,如果不考虑路由的变化,我怎样才能要求结果?

1 个答案:

答案 0 :(得分:0)

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

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