The issue you're experiencing is a common one when using Ziggy with Inertia.js and Vue 3, especially with SSR (Server-Side Rendering). The root cause is that the route helper provided by Ziggy is only available in the template context (via global properties), but not automatically in the <script setup> context.
Why does route work in <template>, but not in <script setup>?
- In the
<template>, Ziggy'sroutehelper is injected as a global property (e.g.,app.config.globalProperties.$route), so you can use it directly. - In
<script setup>, you must explicitly import or inject it, because global properties are not automatically available.
Solution
1. Import route from Ziggy
In your <script setup>, import route directly from Ziggy:
import { route } from 'ziggy-js';
Now you can use route('dashboard') in your <script setup>.
2. If you need Ziggy's config (for SSR)
If you need to use the Ziggy config (for example, for named routes or URL generation that depends on the current location), you can also import Ziggy:
import { route, Ziggy } from 'ziggy-js';
If you need to pass the Ziggy config from the server (SSR), you can access it via your page props:
const ziggyConfig = defineProps(['ziggy']);
And then:
route('dashboard', undefined, undefined, ziggyConfig);
But in most cases, just importing route is enough.
3. Example Usage
<script setup>
import { route } from 'ziggy-js';
const dashboardUrl = route('dashboard');
</script>
4. Why doesn't use(ZiggyVue) make it available in <script setup>?
The use(ZiggyVue) plugin only injects the helper into the global Vue instance, which is accessible in templates or via getCurrentInstance().appContext.config.globalProperties in classic scripts. The <script setup> syntax is more isolated and does not automatically inherit global properties.
Summary
- In
<template>: You can useroute('dashboard')directly. - In
<script setup>: You mustimport { route } from 'ziggy-js'and use it.
References:
Let me know if you need a more detailed example!