【问题标题】:Has anyone received a compile error that makes you use , instead of ; in an axios call?有没有人收到让你使用的编译错误,而不是 ;在 axios 调用中?
【发布时间】:2019-03-28 17:38:58
【问题描述】:

除非我在 axios 调用中使用逗号,否则编译错误。

这是错误。 '意外的令牌,应为“,”'

在 vue 中我有一个 axios 调用。

axios
            .get('/api/messages/'+this.issue)
            .then(response => (

                this.messages = response.data;

                console.log(response.data);

            ))
            .catch(error => console.log(error));

如果我不使用逗号,我会收到编译错误。

axios
            .get('/api/messages/'+this.issue)
            .then(response => (

                this.messages = response.data,

                console.log(response.data)

            ))
            .catch(error => console.log(error));

如果我尝试在 .then() 中运行 if() 语句,也会出现编译错误。我正在使用最新版本的 Laravel、Vue 和 Axios。其他人有这个问题吗?或者有什么解决办法?

【问题讨论】:

  • 那是因为您在 then 回调中使用 () 而不是 {}

标签: laravel vue.js axios


【解决方案1】:

问题

您的错误是因为 javascript 期望 response => () 是一个返回的语句。

你可以把它想象成response => return (/* code */)

解决方案

相反,为了使用没有立即返回的箭头函数,请切换到括号:

response => {}

这样,javascript 不再期望立即返回的语句,而是像函数一样执行 js 的全部功能。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2022-06-17
    • 2015-10-13
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多