Using router.push in file axios (in files vue I can use) #10731
-
import axios from "axios"
import { getCookie } from "../helpers/cookie"
const api = axios.create({ baseURL: 'URL' })
api.defaults.headers.common['Accept'] = 'application/json';
api.defaults.headers.common['Content-Type'] = 'application/json';
api.defaults.headers.common['Authorization'] = `Bearer ${getCookie('TOKEN')}`;
api.interceptors.request.use(function (config) {
return config;
}, function (error) {
return Promise.reject(error);
});
api.interceptors.response.use(function (response) {
return response;
}, function (error) {
if (error.response.status === 401) {
// send to LOGIN
console.log('HERE');
}
return Promise.reject(error);
});
export { api } I've tried everything to use the saw router Or when I import the .push doesn't exist or I can't import (of course, I'm doing it wrong) It should be something like this: Later: router.push({
name: 'login'
}) But not working 😓 In vue files, I do it like this and it works: import { useRouter } from "vue-router";
...
const router = useRouter();
...
router.push({ path: "/login" }); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
What you need is boot file: import { boot } from 'quasar/wrappers'
import axios from 'axios'
const api = axios.create({ baseURL: 'https://api.example.com' })
export default boot(({ app, router }) => {
// for use inside Vue files (Options API) through this.$axios and this.$api
app.config.globalProperties.$axios = axios
// ^ ^ ^ this will allow you to use this.$axios (for Vue Options API form)
// so you won't necessarily have to import axios in each vue file
app.config.globalProperties.$api = api
// ^ ^ ^ this will allow you to use this.$api (for Vue Options API form)
// so you can easily perform requests against your app's API
})
export { axios, api } |
Beta Was this translation helpful? Give feedback.
-
@MilosPaunovic For those who are going to use the axios interceptor, here is a tip: import { boot } from 'quasar/wrappers'
import axios from "axios"
import { getCookie } from "../helpers/cookie"
const api = axios.create({ baseURL: 'URL' })
api.defaults.headers.common['Accept'] = 'application/json';
api.defaults.headers.common['Content-Type'] = 'application/json';
api.defaults.headers.common['Authorization'] = `Bearer ${getCookie('tkadm_pedds')}`;
export default boot(({ app, router }) => {
api.interceptors.request.use(function (config) {
return config;
}, function (error) {
return Promise.reject(error);
});
api.interceptors.response.use(function (response) {
return response;
}, function (error) {
if (error.response.status === 401) {
router.push({
path: '/login'
})
}
return Promise.reject(error);
});
})
export { api } Ahh in quasar.conf.js have edit boot: [
'client'
], I use one instance (from axios) for those who are logged in (with token) and one for those who are not (for login) |
Beta Was this translation helpful? Give feedback.
@MilosPaunovic
Now, I think I got the hang of it.
For those who are going to use the axios interceptor, here is a tip: