【问题标题】:An error in the axios (vue.js), namely "id=[object%20Object]"axios(vue.js)出现错误,即“id=[object%20Object]”
【发布时间】:2021-01-20 20:16:04
【问题描述】:

作为参数,我有一个“测试”id,我将它传递给存储,即 index.js。在控制台中,我有这个错误 -> users?id=[object%20Object]。我已经尝试过->“this.id.toString()”,但它不起作用。有人可以帮帮我吗?

用户.vue

<script>
import { mapActions } from "vuex";
export default {
  data: () => ({
     id: "test",
  }),
  methods: {
    ...mapActions("Test", [
      "GET_USER_BY_ID",
    ]),
    add() {
      this.GET_USER_BY_ID(this.id);
    },
  },
};
</script>

index.js

import axios from '../../plugins/axios'

const actions = {
    GET_USER_BY_ID(userId) { 
    console.log(userId)
        return axios.get(`users?id=${userId}`)
            .then((response) => {
                console.log(response)
                return response
            })
}

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}

【问题讨论】:

  • [object Object] 是当您尝试将对象视为字符串时得到的(因此添加 .toString() 与您应该做的相反)。

标签: javascript vue.js axios vuetify.js


【解决方案1】:

vuex的actions中的第一个参数是一个作用域对象,试试这样:

const actions = {
  GET_USER_BY_ID(_, userId) {
    ...
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2012-05-27
    • 2021-05-19
    • 2022-11-17
    相关资源
    最近更新 更多