我在 Vue.js 中有一个应用程序,其中有一个像这样的 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>
我可以以某种方式从其他组件访问用户变量吗?我应该如何导出和导入它才能成功?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
$root.someVariable在 Vue 2 中工作,未在 3 中测试。
你可以:
用户移至共享状态解决方案,例如Vuex或Pinia