DEV Community

Discussion on: Building Vue.js Client SPA Token-Based Authentication with Laravel Sanctum

Collapse
 
defji profile image
Balázs Györkös

Hi! Nice tutorial! How to use axios in other compoents for further api requests?

Collapse
 
romanpaprotsky profile image
Roman Paprotsky

That's a good question, Balázs!

You can add in main.js:

Vue.prototype.$axios = axios
Enter fullscreen mode Exit fullscreen mode

After that in components you can write:

$axios.get('/items')
  .then(function (response) {
    console.log(response)
  })
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ohidurbappy profile image
Ohidur Rahman Bappy • Edited

From child component I had to refer using this keyword

this.$axios.get('/items')
  .then(function (response) {
    console.log(response)
  })
Enter fullscreen mode Exit fullscreen mode