nuxt如何在其它js文件中使用store

2020-11-11 15:05:39

在新建的js文件中想用store里面的数据,比如token想在封装的axios里面,请求头里面去使用,亦或者通过app的JS接口获取token并存储在store里面。我们都知道如何在vue中如何使用。

前言

在新建的js文件中想用store里面的数据,比如token想在封装的axios里面,请求头里面去使用,亦或者通过app的JS接口获取token并存储在store里面。我们都知道如何在vue中如何使用。

代码

/*
 * @Description: 
 * @Author: lxc
 * @Date: 2019-07-02 16:14:07
 * @LastEditTime: 2019-08-14 16:08:19
 * @LastEditors: lxc
 */// 导出 store 的地方import Vue from 'vue'import Vuex from 'vuex'import state from './state'import actions from './actions'import mutations from './mutations'import getters from './getters'import canteen from './modules/canteen'import contact from './modules/contact'import health from './modules/health'import scan from './modules/scan'Vue.use(Vuex)let storeconst initStore = () => {
  return store || (store = new Vuex.Store({
    // 存放公用数据
    state,
    // 异步操作要通过actions,否则通过cimmit直接操作mutations
    actions,
    // 同步放数据
    mutations,
    getters,
    modules: {
      // store 模块....
    }
  }))}export default initStore

其它js文件中如何调用:

import store from '@/store'const TOKEN = 'testToken'//  这里只是举个例子function getToken() {
  return isNotEmpty(store().state.token) ? store().state.token : TOKEN}



  • 2019-07-11 11:35:20

    VirtualBox添加新硬盘

    昨晚在自己的virtualbox中的linux装matlab2010a,没想到硬盘空间不足,所以找了下怎么添加硬盘的资料。也很简单,每几条命令。 大致流程:我的virtualbox版本是3.2.8,linux版本是xubuntu10.01吧貌似

  • 2019-07-23 14:49:40

    Windows10上使用Linux子系统(WSL)

    Linux的Windows子系统让开发人员可以直接在Windows上运行Linux环境(包括大多数命令行工具,实用程序和应用程序),而无需建立在虚拟机的开销之上,整个系统共200多M,但包含了你能用到的所有功能,并且和windows完美互操作(省去linux挂载本地windows分区或目录的操作),目前Linux的windows子系统已经相当完善,可当作完整linux系统使用

  • 2019-07-24 01:21:15

    android开发无障碍app

    最近做一些为盲人提供服务的APP,还是挺有感触的,感谢手机和互联网的普及,他们的生活比以前丰富了很多。 通过读屏软件,盲人可以操作手机,上网浏览信息。读屏软件的工作原理很简单,就是读出屏幕上按钮、文本的信息。