title: How to access Vue component data and router from external javascript functions?
body: <p>I am new to Vue. I am trying to build a sort of middleware where I can check for all the status codes and handle it the wait I want.</p>
  <p>I want to use this function everywhere I send a request to the backend. To prevent code duplication, im using a middleware function.</p>
  <p>the function:</p>
  <pre><code>module.exports.serverErrorMiddleware = async (res)=&gt;{  
     const status = res.status;
     if(status == 500) return router.push('/error') //problem 1
     if(status == 404) return router.push('/notfound')
     if(status == 302) return router.push('/login')
     if(status == 403 &amp;&amp; !(res.error.isrefTokenError)) return router.push('/unauthorized')
     if(status == 400) this.error = res.error.message  // problem 2
  
     if(status == 401){
     if(!localStorage.getItem('refresh_token')) return router.push('/login')
       try{
          const response = 
          await axios({
              method : 'get',
              url    : 'http://localhost:5000/api/auth/newtoken'
          })
              localStorage.setItem('access_token' , response.auth.accessToken);
      }catch(err){
          if(err.response.status == 403 &amp;&amp; err.response.error.isrefTokenError) return router.push('/login');
          router.push('/error')
      }
  }
  }
  </code></pre>
  <p>problem 1 : I want to push a new route into the <code>router</code> for redirects but dont know how to access the <code>router</code></p>
  <p>problem 2 : I want to change the <code>data</code> in the Vue component from this function. I want to show the error to the user according to the error I receive from the server. So I want to access the <code>error</code> property in <code>data</code> of the component this function is called from.</p>
  <p>I call this function in the <code>catch</code> of <code>axios</code> request</p>
  <pre><code> async submitForm(event){
          event.preventDefault()
  
          try{
               const response = await axios({
                  method : 'post',
                  url : 'http://localhost:5000/api/auth/register',
                  data : {
                      username : this.formData.username,
                      password : this.formData.password
                  }
               })
               console.log(response)
          }catch(err){
              serverErrorMiddleware(err.response)
          }
      }
  </code></pre>