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}



  • 2020-12-03 16:25:34

    WSL安装及升级WSL2

    幸好我们有了WSL(Windows Subsystem for Linux),顾名思义就是Windows中可以用Linux了,当然命令也会丰富更多,尤其是WSL2的发布,使得更多原来只能在Linux中做的事情现在也可以在Windows中干了。那么接下来我们就分别介绍WSL和WSL2的安装。

  • 2020-12-06 16:46:11

    git撤销pull

    刚刚不小心pull了一下,有错误,想撤回怎么办。

  • 2020-12-06 19:05:13

    visual studio 配置 intellij idea快捷键

    我原本从intellij idea转换到visual studio是因为webstorm没办法远程开发,而visual studio有remote wsl,和remote ssh,看着挺不错的样子。

  • 2020-12-06 20:38:30

    intellij idea远程开发remote

    开发时一般的平台都是windows,但windows对开发极其不友好,一般都会在本地开启虚拟机,安装上linux环境进行项目的部署测试。下面介绍一种windows主机与linux虚拟机代码同步的方法。这个工具适用于jerbrains公司旗下的很多产品,比如idea、webstrom、phpstrom等。但是要注意你安装的IDE必须是专业版的,社区版的IDE是没有这个代码同步功能的哦!

  • 2020-12-07 05:46:56

    npm设置和取消代理的方法

    有时候是设置了全局代理对npm并不生效,不如直接给npm设置代理,至少在mac电脑我是有这种感觉的。