반응형
How to use store in Vue/ Nuxt plugin?
I am writing a nuxt app that authenticates with a backend. I have a http plugin that intercepts all http requests. I need to add an auth token to all requests, the token is in the store. What I want to know is, how do I access the store from the plugin?
import axios from 'axios';
var api = axios.create({
baseURL: 'http://127.0.0.1:8000/api/'
});
api.interceptors.request.use(function (config) {
config.headers = {
'Authorization': 'Bearer' + **how do access store?**
}
return config;
}, function (error) {
return Promise.reject(error);
});
export default api;
Thanks
You can try to use app store from context in plugin. Your plugin need some changes:
import axios from 'axios';
var api = axios.create({
baseURL: 'http://127.0.0.1:8000/api/'
});
export default (context, inject) => {
api.interceptors.request.use(function (config) {
config.headers = {
'Authorization': 'Bearer' + context.app.$store.state.your_path_to_token
}
return config;
}, function (error) {
return Promise.reject(error);
});
return api;
}
One more way it's create store/index.js file and import them into plugin.
ReferenceURL : https://stackoverflow.com/questions/50233438/how-to-use-store-in-vue-nuxt-plugin
반응형
'programing' 카테고리의 다른 글
Java에서의 SOAP 및 RESTful 웹 서비스의 주요 차이점 (0) | 2022.07.27 |
---|---|
v-html을 VUE 구성 요소로 렌더링 (0) | 2022.07.27 |
왜 memset은 char가 아닌 int를 사용합니까? (0) | 2022.07.27 |
VueJ의 동적 컴포넌트에 소품을 동적으로 전달s (0) | 2022.07.27 |
C - char 배열에 char가 있는지 확인합니다. (0) | 2022.07.27 |