我創(chuàng)建了一個 Vue 應(yīng)用程序,其中有一個 HTTP 請求。數(shù)據(jù)返回后,我會更新配置文件值。但是使用此值的所有組件都不會重新加載。下面是更好地解釋我想要完成的代碼。我有主要的 Vue 文件 App.vue:<template> <div id="app"> <Navigation /> <Header :profile="profile" /> <About :profile="profile" /> <Services :profile="profile" /> </div></template><script>import Navigation from './components/Navigation.vue'import Header from './components/Header.vue'import About from './components/About.vue'import Services from './components/Services.vue'export default { name: 'app', components: { Navigation, Header, About, Services, }, data() { return { profile: { } } }, created() { this.getUserProfile() }, methods: { getUserProfile: async function() { try { const response = await fetch('http://localhost:7070/v1/home'); const data = await response.json(); this.profile = data; } catch (error) { console.error(error); } } }}</script><style></style>如您所見,我在開始時將變量配置文件設(shè)置為空對象。一旦應(yīng)用程序進入掛載狀態(tài),我就可以通過 GET 請求檢索配置文件數(shù)據(jù)。當(dāng)我調(diào)試時,我可以清楚地看到數(shù)據(jù)不是一個空對象。響應(yīng)包含所有數(shù)據(jù)。正如您從應(yīng)用程序文件中看到的,我導(dǎo)入了 4 個文件以向應(yīng)用程序添加 4 個組件。所有組件都以相同的原理完成。這是 navigation.vue 的內(nèi)容:<template> <nav class="navbar navbar-expand-lg navbar-light fixed-top py-3" id="mainNav"> <div class="container"> <a class="navbar-brand js-scroll-trigger" href="#page-top">Home</a> <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav ml-auto my-2 my-lg-0"> <li class="nav-item"> <a class="nav-link js-scroll-trigger" href="#about">About</a> </li> <li class="nav-item"> <a class="nav-link js-scroll-trigger" href="#services">Services</a> </li>
組件的值未更新
哈士奇WWW
2021-09-30 13:57:00