What is the process for inputting data into a Inertia component?

I have a simple component:

<script setup>
import {ref} from 'vue'

const labels = []
</script>

<template>
<div v-for="(label, index) in labels" :key="index" v-html="label"></div>
</template>

My goal is to incorporate this Component into an <aside> block displaying "side information". I want this Component to have its own data, separate from the parent element, so I can include it anywhere.

I know how to create a "full page" component where I pass data using

inertia('Page', ['labels' => ['Foo', 'Bar']])
from the controller to the vue component.

However, I am unsure of how to achieve this for the "side" component.

I have set up a route to an invokable controller that would send the data as json, but this would require me to load the data using axios in the traditional vue way.

Now I am questioning how I can accomplish this with inertia. I am aware of manual visits using router from inertiajs/vue, but the get method seems more geared towards "navigating away" rather than "fetching data".

Answer №1

If you want to utilize the concept of Shared data, you can follow this approach:

// HandleInertiaRequests.php
public function share(Request $request)
{
    return array_merge(parent::share($request), [
        'labels' => ['Foo', 'Bar'],
        ...
    ]);
}
<!-- Vue component -->
<script setup>
import { computed } from 'vue'
import { usePage } from '@inertiajs/vue3'

const page = usePage()

const labels = computed(() => page.props.labels)
</script>


<template>
    <div v-for="(label, index) in labels" :key="index" v-html="label"></div>
</template>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Bootstrap Mobile Navigation transition is not displaying as intended in my CSS coding

On both desktop and mobile screen sizes, I have a white navigation bar. However, for the mobile dropdown menu, I want the background to be grey. I managed to achieve this by targeting .show on smaller screens in the CSS and specifying the grey background ...

Encountering issues with Auth0 route guard functionality when using Nuxt middleware

How can I properly implement Auth0 route guards in Nuxt? After adjusting the Auth0 sample code, I have created a middleware as shown below: import {getInstance} from '~/plugins/auth'; export default function () { const authService = getInstan ...

The function successfully triggers when clicked using (React, JS, TS) but does not respond to key presses

When the function is called with "onClick", it works correctly, but when called with "onKeyPress", it does not execute an if statement. scenario Consider a scenario where you can search for food recipes (e.g. "pizza") and receive a list of recipes from a ...

Insert newly added rows' values into the database dynamically

Hello there, I'm currently working on a PHP form that needs to dynamically add a table row when the "Add" button is pressed. I'm using a for loop to save the values, but I'm running into an issue where the data is not being saved into my dat ...

Issue with Google map displaying incorrectly when attempting to print

I am experiencing an issue with printing a Google Map using the JavaScript API V3. The problem I am facing is that the map is split into two parts, with the top and bottom sections overlapping each other. Here is the code snippet: var geocoder; var ...

Change the local date and time to UTC in the format of yy:mm:dd H:M

I must change the date format from local time to UTC or ISO as yy:mm:dd H:M, or calculate the difference between local date times with 03:30 as yy:mm:dd H:M 2016-10-22T04:30:00.000Z convert this to 2016-10-22T01:00:00.000Z ...

Can we utilize the elements in Array<keyof T> as keys in T?

Hello, I am trying to develop a function that accepts two parameters: an array of objects "T[]" and an array of fields of type T. However, I am encountering an issue when I reach the line where I invoke el[col] Argument of type 'T[keyof T]' i ...

Navigating items with Vuetify and V-Virtual-Scroll: A guide to programmatically scrolling into view

Given that I have the height of my item and its position in the list, it should be possible for me to determine the scroll position. Still, I am uncertain about how to actually adjust the scroll position. ...

Instructions for developing an offset plugin for an HTML5 video player

I'm currently working on developing an offset plugin that allows playing a specific portion of a video in an HTML5 video player. While there is an existing plugin for video.js available at videojs-offset plugin, I am now experimenting to adapt this p ...

Jquery allows for the toggling of multiple checkboxes

I have a group of check-boxes that I would like to toggle their content when checked. Currently, I am using jQuery to achieve this functionality, but I am searching for a way to optimize my code so that I do not need to write a separate function for each ...

Effective ways to manage extensive forms in Angular 2

I have a complex form in my application. What is the most effective way to collect data from this form and transmit it to the API using Angular 5? ...

The behavior of Date's constructor differs when applied to trimmed versus untrimmed strings

Although it's commonly advised against creating a date from a string, I stumbled upon an interesting phenomenon: adding a space before or after the date string can impact the resulting date value. console.log([ new Date('2019-03'), ...

Converting user input from a string to an object in JavaScript: a comprehensive guide

Is there a way to transform user input string into objects when given an array of strings? ["code:213123", "code:213123", "code:213123"] I am looking to convert this array into an array of objects with the following format: [{code: "213123"},...] ...

Encountering a problem while integrating highchartsjs into a Vue3 application

I'm currently using Vue 3 and I followed the documentation to add highchartsjs to my project. However, I encountered an error: ✘ [ERROR] Could not resolve "highcharts" node_modules/highcharts-vue/dist/highcharts-vue.min.js:1:90: 1 │ ... ...

access various paths to distinct iframes

<?php // Specify the directory path, can be either absolute or relative $dirPath = "C:/xampp/htdocs/statistics/pdf/"; // Open the specified directory and check if it's opened successfully if ($handle = opendir($dirPath)) { // Keep readin ...

What steps can I take to address the issue of missing @angular/Core modules?

I am encountering an issue with running my Angular 2 project. Here's what I have tried: - Attempted to run the project using npm install and npm start, but it did not work - Cloned a quickstart from Github and replaced it with my src folder, only to ...

Tips for determining the actions of a node prior to its inception

Is there a way to automatically run scripts on specific elements after their creation? For example, using a jQuery plugin like autoresize to expand the height of a textarea. If I use $('.textarea').autosize(), only the current textareas will be a ...

Display/Conceal JavaScript

I recently implemented a JavaScript function on my website to show/hide specific elements. However, being new to JavaScript, I have encountered some difficulties. I've spent quite some time troubleshooting the code but haven't been able to pinpoi ...

Preserving the true IP address when initiating cross-domain requests

Initially, I tried creating a reverse proxy using Express to enable forwarding requests from localhost:3000/request to somesite.com/request. Here is the code snippet I used: var request = require('request'); app.get('/', function(req, ...

Is it possible for Prototype to enhance SVG components?

Having some issues developing an interactive SVG map with Prototype. Extending inline SVG elements seems to be causing problems. Take a look at my code snippet below (path data removed for brevity): <svg id="map" xmlns="http://www.w3.org/2000/svg" xmln ...