ziggy: Documentation is missing the requirement of `vendor/tightenco/ziggy/src/js/route.js`
Expected Behavior
Documentation should explain that the route.js
file needs to be imported from vendor/tightenco/ziggy/src/js/route.js
when using the ziggy:generate
artisan command.
Current Behavior
Not documented.
I’ve put together an example for anyone that uses Laravel Mix and Vue (probably a lot of people who want to use Ziggy!)
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 21 (1 by maintainers)
When using
Vue.mixin()
, you are essentially adding something to every component definition, so the following snippet:adds a
route()
method to every component (accessible withthis.route('route.name.here')
.For your example, you don’t need to assign
route
at all in thedata() { ... }
portion of your component. You can fix yourimportUser()
method by simply usingaxios.post(this.route('users.store'))
.To use
route()
outside Vue components:There are times where I need to use the route method outside of Vue components. For this, i use the following solution:
Make a separate
route.js
file that you can import wherever you need it:Then you can import it into
app.js
to set up the global mixin:Or in any other module / js file e.g. a VueX store:
Depending on your Laravel version, you may need to change the path of the
ziggy:generate
command as it defaults to the ‘legacy’resources/assets/js/
directory. Laravel >=5.8 (I think) usesresources/js/
instead.I’ve just done a fresh Laravel install and these are the exact steps taken to get Ziggy to work with Vue:
Set up in terminal:
webpack.mix.js
routes/web.php
Run Ziggy
resources/js/app.js
** resources/js/components/ExampleComponent.vue **
Add built assets to your blade layout
Add Vue component to your blade view
Build assets
I haven’t used this package for a couple of years so something may have changed since my previous comments. I’m not sure without digging into it, but I don’t have time at the moment I’m afraid, sorry about that.
On Sat, 4 Dec 2021 at 05:46, Peter Weil @.***> wrote:
@aFluxx I haven’t had any issue using the file from
dist
.If you reference the
route.js
file from thesrc
directory, you will most likely have to update your webpack config to ensure thebabel-loader
will include that file to transpile the JS into a browser friendly ES5 syntax.Requiring the
route.js
from thedist
directory won’t have this problem as it’s already been transpiled.