php - InertiaJS: keep current component - Stack Overflow

admin2025-04-16  3

I can't wrap my head around how InertiaJS works...

I am using a vanilla PHP backend solution, so all the magic revolving around frameworks like Laravel can't help me.

I would like to submit a form (using ajax then), and get the errors... Which means I am staying on the same component if I ever have to display any error...

However, a proper InertiaJS requires a "component" prop, but for ease of use I would like to skip specifying the "component" (since it is the same...), it would make my code less redundant. But it won't work.

I also did not understand the "redirect" part of error handling that is discussed in the docs... I am returning a standard Inertia response, with a component and props, but no redirect (and it works nicely as long as I provide the component name).

How can I handle ajax errors, without having to specify a component in my response? (i.e. telling Inertia "please use the current component")

(front is using Svelte4 and the official svelte adapter, but not sure it matters)

Thank you kindly for any help,

I can't wrap my head around how InertiaJS works...

I am using a vanilla PHP backend solution, so all the magic revolving around frameworks like Laravel can't help me.

I would like to submit a form (using ajax then), and get the errors... Which means I am staying on the same component if I ever have to display any error...

However, a proper InertiaJS requires a "component" prop, but for ease of use I would like to skip specifying the "component" (since it is the same...), it would make my code less redundant. But it won't work.

I also did not understand the "redirect" part of error handling that is discussed in the docs... I am returning a standard Inertia response, with a component and props, but no redirect (and it works nicely as long as I provide the component name).

How can I handle ajax errors, without having to specify a component in my response? (i.e. telling Inertia "please use the current component")

(front is using Svelte4 and the official svelte adapter, but not sure it matters)

Thank you kindly for any help,

Share Improve this question asked Feb 3 at 20:08 Aghyul KyokuAghyul Kyoku 1,05810 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

You can use Axios to make a request to your backend and use a state variable to get the response and either display it or process it in a table or a view, pretty much anything you want to do. I use InertiaJs and PHP Laravel.

Here is an example below...

<script>
const fetchData = async () => {
    try {
        isLoading.value = true;
        error.value = null;
        const response = await axios.get(route('family.index'));
        apiData.value = response.data.data;
    } catch (err) {
        console.error('Error fetching data:', err);
        error.value = 'Failed to load family tree data. Please try again later.';
    } finally {
        isLoading.value = false;
    }
};

const totalUsers = page.props.totalUsers
const totalFamilyMembers = page.props.totalFamilyMembers
const totalPartners = page.props.totalPartners

onMounted(() => {
    fetchData();
});
</script>
转载请注明原文地址:http://www.anycun.com/QandA/1744752135a87110.html