Home > Net >  Vue.js, How to send object in api link via axios?
Vue.js, How to send object in api link via axios?

Time:10-27

So I have GET|HEAD api: api/users/{user}, and it calls function App\Http\Controllers\Api\UserController@show. This function in Laravel is:

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

How to send variable id to my function. 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.

CodePudding user response:

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 ``

CodePudding user response:

I think you should use ` instead of ' so its possible to pass {this.$route.params.id} ;


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

  • Related