Javascript 来自Laravel API的Vuejs全局用户对象
我有一个带有单文件组件的小型Laravel/VueJS应用程序,它是一个单页应用程序。我用于中央国家管理 这是我的商店Javascript 来自Laravel API的Vuejs全局用户对象,javascript,php,laravel,vue.js,Javascript,Php,Laravel,Vue.js,我有一个带有单文件组件的小型Laravel/VueJS应用程序,它是一个单页应用程序。我用于中央国家管理 这是我的商店 export default { user: { name: null } } 这是我的仪表板上的api调用 <script> export default { mounted() { axios.get('/api/getAuthUser')
export default {
user: {
name: null
}
}
这是我的仪表板上的api调用
<script>
export default {
mounted() {
axios.get('/api/getAuthUser')
.then(response => {
this.$store.user.name = response.data.name;
});
}
}
</script>
这是行不通的。名称仍然为空
//更新2
import axios from 'axios'
axios.defaults.headers.common = {
'X-CSRF-TOKEN': Laravel.csrfToken,
'X-Requested-With': 'XMLHttpRequest'
};
export default {
user: {
name: null
},
setUser() {
axios.get('/api/getAuthUser')
.then(response => {
this.user.name = response.data.name;
});
}}
这是我的完整store.js,但似乎setUser()没有被执行。如果您需要在每个页面上使用您的用户,那么类似这样的东西怎么样
new Vue({
el: "#app",
mounted: function() {
// make a request to the api here
},
});
如果你需要你的用户在每一页上,那么像这样的东西呢
new Vue({
el: "#app",
mounted: function() {
// make a request to the api here
},
});
您有根组件吗?您应该在根组件中进行API调用并将其传递给其他组件。或者将API调用传递到存储本身,然后在需要的位置导入存储并显示所需的数据。我如何直接从存储中进行API调用?查看我的更新是否有根组件?您应该在根组件中进行API调用,并将其传递给其他组件。或者将API调用传递到存储本身,然后在需要的位置导入存储并显示所需的数据。我如何直接从存储中进行API调用?查看我的更新