简体   繁体   English

Vue.js,如何通过 axios 在 api 链接中发送对象?

[英]Vue.js, How to send object in api link via axios?

So I have GET|HEAD api: api/users/{user} , and it calls function App\\Http\\Controllers\\Api\\UserController@show .所以我有GET|HEAD api: api/users/{user} ,它调用函数App\\Http\\Controllers\\Api\\UserController@show This function in Laravel is: Laravel 中的这个函数是:

   public function show($id){
    return new UserShowResource(User::findOrFail($id));
}

How to send variable id to my function.如何将变量id发送到我的函数。 I found somewhere this:我在某处找到了这个:

const helpVar = axios.get('/api/users/{this.$route.params.id}').then(response => {
           this.users = response.data;
           this.loading = false;
             
        });

But it does not work.但它不起作用。 I tried axios.get('/api/users/1') and it returns me user with id=1 , so problem is with this link in axios.我试过axios.get('/api/users/1')并且它返回给我id=1用户,所以问题出在 axios 中的这个链接上。

Your code isn't working because your are using simple quotation marks.您的代码不起作用,因为您使用了简单的引号。 As you are trying to use template literals for interpolating you must change your code to:当您尝试使用模板文字进行插值时,您必须将代码更改为:


const helpVar = axios.get(`/api/users/{this.$route.params.id}`).then(response => {
           this.users = response.data;
           this.loading = false;
             
        });
  • Notice the change of '' to ``注意将 '' 更改为 ``

I think you should use ` instead of ' so its possible to pass {this.$route.params.id} ;我认为你应该使用 ` 而不是 ' 这样就可以通过 {this.$route.params.id} ;


const helpVar = axios.get(`/api/users/{this.$route.params.id}`).then(response => {
           this.users = response.data;
           this.loading = false;
             
        });

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

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