[Vuejs]-VueJs Call plugin from .js file

0👍

What if you remove the catch part of your axios function

return axios.post(`${serv}/${ressource}`,
{
    data: params,
},
{headers: {'Authorization': 'Bearer ' + token}})
.then((response) => response = response.data)

and catch it where you want to show the toast. Login.vue:

function logUser () {
  return postRequest()
    .then(responseData => {
      //do your stuff
    })
    .catch((err) => {
      this.$toast.error(err.response.data.message)
      throw err.response.data.message
    })
}

Update:

The solutions is actual pretty easy. You just need add your method where this needs to be the vue instance to your component.

See following example:

accessVueInstance.js:

export function compare(vueInstance) {
  return this === vueInstance
}

MyComponent.vue:

import the function

import { compare } from "./accessVueInstance"

add the function to your component:

if you use class components:

export default class MyComponent extends Vue {
  compare = compare

  created() {
    console.log(this.compare(this)) //will log true
  }
  //your stuff
}

else:

methods: {
  compare
},
created() {
  console.log(this.compare(this)) //will log true
}

vue docs

Vue automatically binds the this value for methods so that it always
refers to the component instance.

See it in action codesandbox.io

Leave a comment