【问题标题】:Pass Data Via Object In Laravel Vue Form通过 Laravel Vue 表单中的对象传递数据
【发布时间】:2020-03-14 23:41:52
【问题描述】:

我想通过对象传递 Vue 表单数据,但是当我传递数据时它会抛出错误

SyntaxError: E:...\js\frontend\Profile.vue: Unexpected token, expected "," (251:4)

这是我的代码

export default {
    props : [
        'user'
    ],
    methods : {
        profile() {
            axios
                .put(`/web/me`, {
                    user.firstName : this.user.firstName,
                    user.lastName  : this.user.lastName,
                    profile.code   : this.user.profile.code,
                })
                .then((e) => {
                    window.location.href = '/profile';
                })
                .catch((e) => {
                    this.errors    = e.response.data.errors;

                })
        },
    }
}

我找不到发送它的方法。提前致谢

【问题讨论】:

  • 您的json文件不正确,请去掉profile.code : this.user.profile.code,最后的逗号
  • @RaminRezazadeh 已经尝试但没有成功

标签: laravel forms vue.js


【解决方案1】:

JSON 结构不正确。你不能使用“。”作为 JSON 结构中的键。 如果用户是一个对象: 你可以像这样使用代码:

export default {
        props : [
            'user'
        ],
        methods : {
            profile() {
                var data = {};
                data[user.firstName] = this.user.firstName;
                data[user.lastName] = this.user.lastName;
                data[profile.code] = this.user.profile.code;
                axios
                    .put(`/web/me`, data)
                    .then((e) => {
                        window.location.href = '/profile';
                    })
                    .catch((e) => {
                        this.errors    = e.response.data.errors;

                    })
            },
        }
    }

用户不是对象:

export default {
        props : [
            'user'
        ],
        methods : {
            profile() {
                var data = {
                  'user': {
                    'firstName': this.user.firstName,
                    'lastName': this.user.lastName,
                  },
                  'profile': {
                    'code': this.user.profile.code
                  }
                };
                axios
                    .put(`/web/me`, data)
                    .then((e) => {
                        window.location.href = '/profile';
                    })
                    .catch((e) => {
                        this.errors    = e.response.data.errors;

                    })
            },
        }
    }

【讨论】:

  • 有没有办法在Json中绑定键值格式的数据?
  • 以上代码抛出错误[Vue warn]: Error in v-on handler: "ReferenceError: user is not defined"
  • 已经做了但抛出错误Error in v-on handler: "TypeError: Cannot set property 'firstName' of undefined"
【解决方案2】:

只需将对象键的名称更改为不带点即可。

user.firstName : this.user.firstName

前一个不是正确的对象键。您需要将其更改为以下内容:

userFirstName : this.user.firstName

【讨论】:

  • 不,先生,我不想要这个。
猜你喜欢
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
  • 2018-09-11
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多