简体   繁体   English

Axios Get与Vuex中的标头-NUXTJS

[英]Axios Get with Header in Vuex - NUXTJS

I get a issues with Axios Get with Header in Vuex - VueJS, hope you guys help me. 我在Vuex-VueJS中遇到有关Axios Get with Header的问题,希望你们对我有所帮助。

Pages: 页数:

<template>
<div class="temp">{{users.info}}</div>
</template>

<script>
data: function () {
    return {
      config: {
        'headers': {'Authorization': 'JWT ' + this.$store.state.token}
      }
    }
  },
fetch ({ store, params }) {
    return store.dispatch('getProfile', params, this.config)
  },
</script>

Vuex Modules: Vuex模块:

import api from '~/plugins/axios'

const state = () => {
  return {
    info: null
  }
}

const actions = {
  getProfile ({commit}, params, config) {
    return new Promise((resolve, reject) => {
      api.get(`/users/${params.username}/`, config)
      .then(response => {
        commit('GET_USER_DETAIL', response.data)
        resolve(response.data)
      },
      response => {
        reject(response.data)
      })
    })
  }
}
const getters = {}

const mutations = {
  GET_USER_DETAIL (state, info) {
    state.info = info
  }
}

export default {
  state,
  actions,
  getters,
  mutations
}

Issues: config in Vuex module is not defined. 问题:Vuex模块中的配置未定义。

I think Im wrong with something hope your help. 我认为我有问题,希望对您有所帮助。 Thanks in advance! 提前致谢!

Actions in Vuex can't contain more than one parameter. Vuex中的动作不能包含多个参数。 Group up your params into a single object, like so: 将参数分组为一个对象,如下所示:

return store.dispatch('getProfile', { params: params, config: this.config })

Then access from your action like so: 然后像这样访问您的操作:

getProfile ({commit}, obj) {
  var params = obj.params
  var config = obj.config
  /* ... */
}

If you look at the section Dispatching Actions in the docs it shows the correct way to pass params. 如果您查看文档中的“调度操作”部分,则显示了传递参数的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM