you can simoly write method and call in in created hook.
//ProfileDropDown.vue
<template>
<div class="text-right leading-tight hidden sm:block">
<p class="font-semibold">{{ activeUserInfo.displayName }}</p>
<small>Available</small>
</div>
</template>
<script>
import axios from 'axios';
export default {
data () {
return {
activeUserInfo: null
}
},
methods: {
async getUserInfo () {
try {
const res = await axios.get('auth/me', {
headers: { Authorization: 'Bearer token' }
});
this.activeUserInfo = res.data.user;
} catch (e) {}
}
},
async created() {
await this.getUserInfo();
}
}
</script>
If you want get data with store, you can use axios in state.js
import axios from 'axios';
...
const getUserInfo = () => {
const userInfo = {}
try {
const res = await axios.get('auth/me', {
headers: { Authorization: 'Bearer token' }
});
userInfo = res.data.user;
} catch (e) {}
return userInfo
}
...
And in your ProfileDropDown.vue file you can save your code
But in this case axios didn't work globally. If you want use it. you have to import axios inmain.js
file and use
//Create vue object
const http = axios.create({baseUrl: 'your base url'});
//set token for all reqeusts
http.interceptors.request.use(config => {
const token: string | null = localStorage.get('token'); //or use what you want
if (token) {
config.headers['Authorization'] = `Bearer ${token}`;
}
config.headers['Accept'] = 'application/json';
config.headers['Content-Type'] = 'application/json';
return config;
},
error => Promise.reject(error)
);
//Create globa axios object for Vue
Vue.prototype.$axios = http;
After that you don't need to import axios anywhere.
Instead of `axios` use `this.$axios` For ex:
...
const res = await this.$axios.get('auth/me')
this. activeUserInfo = res.data.user;
...
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Vue is an open source JavaScript framework for building a creative interface. Integration with Vue in projects using other JavaScript libraries is simplified because it is designed to be adaptable. Over 36,000 websites currently use Vue. Companies like Stackoverflow, PlayStation, etc. rely on Vue for their UI sites.
https://vuejs.org/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.