Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the steps to inject initial application state into the HTML page
    Laravel Vue.js initial application state server-side rendering JSON Blade template Vue instance web development


What are the steps to inject initial application state into the HTML page


The steps to inject initial application state into the HTML page are:

1. Prepare the Initial State on the Server-side:
- In your Laravel controller, fetch the necessary data to populate the initial state of your Vue.js application.
- Encode the data as JSON and assign it to a global variable in the HTML template.

php
   // app/Http/Controllers/CarsController.php
   public function index()
   {
       $cars = Car::all();
       $initialState = json_encode(['cars' => $cars]);
       return view('welcome', ['initialState' => $initialState]);
   }
   

2. Inject the Initial State into the HTML Template:
- In your Blade template, add a `