我在 Vue.js 中有一個(gè)應(yīng)用程序,其中有一個(gè)像這樣的 App.vue:
<template> <div id="app"> <router-view/> </div> </template> <script> export default { name: 'App', data() { return { user: null, } }, methods: { getUser() { axios .get(`auth/user/`) .then(response => { this.user=response.data.pk }) } }, beforeMount() { this.getUser(); }, } </script>
我可以以某種方式從其他元件存取使用者變數(shù)嗎?我應(yīng)該如何匯出和匯入它才能成功?
你可以:
使用者
移至共享狀態(tài)解決方案,例如Vuex或Pinia