How to load all server side data on initial vue.js / vue-router load?

Cover Image for How to load all server side data on initial vue.js / vue-router load?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 How to Load All Server Side Data on Initial Vue.js/Vue Router Load

Are you facing the issue of your server side data loading only after the page has rendered when using Vue.js and vue-router? This can be frustrating, especially when you want to load all route and page data on the initial page load. But fret not, we've got you covered!

🌐 The Problem

As mentioned in the context, there's a single-page site built with Vue.js and vue-router, making use of the WordPress REST API. However, the data loads only after the page has rendered when making an AJAX call to the server using the REST API. This can lead to delayed data display and a poor user experience.

💡 The Solution

To load all server side data on the initial page load, we need to make a few changes to our code.

  1. Update main.js Router Code:

    In the created() method of your main.js file, make the AJAX call to fetch the data from the server. Instead of directly assigning the response to this.acfs, we will create a Promise that resolves when the data has loaded. This way, we can ensure that the route components receive the data before rendering.

    const app = new Vue({ router, data: { acfs: '' }, created() { const loadData = () => { return new Promise((resolve, reject) => { $.ajax({ url: 'http://localhost/placeholder/wp-json/acf/v2/page/2', type: 'GET', success: function(response) { console.log(response); // Assign the response to acfs data property this.acfs = response.acf; resolve(); }.bind(this), error: function(error) { reject(error); } }); }); }; // Call the loadData function and wait for the promise to resolve loadData().then(() => { // Once the data has loaded, mount the Vue app this.$mount('#app'); }).catch((error) => { console.error(error); }); } });
  2. Update Home.vue Component Code:

    In your Home.vue component, update the code to access the data from the acfs property of the parent component.

    export default { name: 'about', data () { return { acf: this.$parent.acfs, } }, }

🎉 Engage with Us!

Now that you know how to load all server side data on initial Vue.js/vue-router load, it's time to level up your Vue.js game! If you found this guide helpful, share it with your fellow developers and encourage them to join the Vue.js community. Feel free to leave comments below if you have any questions or want to share your own experiences. Happy coding! 👩‍💻👨‍💻


Reference: Vue.js Documentation - Data Fetching


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello