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}



  • 2017-11-10 00:06:15

    CORS: credentials mode is 'include'

    XMLHttpRequest cannot load http://localhost/Foo.API/token. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:5000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

  • 2017-11-19 00:17:51

    Java如何获取Date的“昨天”与“明天”示例代码

    最近在做项目的时候用到Date和Calendar比较多,而且用到的方式也比较全,突然想到一个问题,Java如何获取Date的"昨天"与"明天",也就是前一天和后一天呢?思考后写出了方法,想着万一以后用到,就总结出来,也方便有需要的朋友们参考借鉴,下面来一起看看吧。