Adding global route helper using groups in Vue Inertia app #652
-
Hello, I'm trying to figure out how I can add the route helper in globally when setting up route grouping in an I have added 2 groups,
Then I edited my app.js / createInertiaApp() to the following:
I looked at other issues & discussion here on this repo & saw that some people had added mixins to make it available in globally & you can see in the commented out section what I tried, I couldn't get it work. I can also see that there is an a route helper in the ZiggyVue (which I guess why it's available in the template), but I am lost for how to make it available in the script setup section. I also tried to inject('route') but it said that route was not defined. Please help! Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
So injecting the route helper into each components has slightly fixed it in a very messy hackish way. I can now use the route helper in the script setup tags, but not inside of defineProps, defineOptions or defineEmit (not sure why?). I don't need to provide the the route helper, I just need to inject it in the component where it is used. However I have another issue, the props were not being updated when switching, from to & then back to the original ziggy config group (so This is the mixin:
There must be a better way to handle this in the app.js file that providers the route helper globally, updates the route groups & doesn't need to use the additional package to do this? I mean surely the route grouping feature would not have been implemented without the ability to utilise it without needing to use the I'm hoping that someone smarter than me can help me work out how I can achieve this just using the ZiggyVue package in the app.js file. Thanks! |
Beta Was this translation helpful? Give feedback.
Injecting
route
in every component is the only way to make it available if you're using the composition API. You can also keep@routes
in your root template and then it'll be available globally, but your IDE might complain about that.If you need the available routes to change on every navigation event then you'll have to either set up your own route function, as you've done, or listen for Inertia's navigation events and manually update the global Ziggy config. Try this too: #567
The simplest option here is probably for you not to use the Vue plugin and to just do what the Vue plugin is doing yourself (so use the
route
function wrapper you've got above andprovide
it globally yourself). Y…