【问题标题】:How to get variable value from other js files in nuxt js如何从nuxt js中的其他js文件中获取变量值
【发布时间】:2018-09-21 08:33:30
【问题描述】:

我有一个 nuxt js 项目,项目文件目录如下:

| -- axios实用程序 | -- index.js | -- 页面 | - 店铺 | -- 其他目录

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 文件(或第三方库)的结果。异步请求的结果保存在哪里,无论路由的变化如何要求结果?

【问题讨论】:

  • 将ajax返回的值保存在vue store中。

标签: javascript vue.js axios vuex nuxt.js


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多