How to use static routes with mix and Vuejs
See original GitHub issueJust starting to use this, and I’m a bit confused about how to use the artisan command in my Vuejs project.
I run the command to generate resources/assets/js/ziggy.js
, but then what? I’ve tried adding require('./ziggy');
to app.js
, but do I still need the blade directive? I think I do in order for the route()
helper function to exist in JS, but then I think all the routing info is duplicated (i.e. the blade directive and ziggy.js
define all the route).
And does the artisan command support groups in my config/ziggy.php
?
Thanks in advance!
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (5 by maintainers)
Top Results From Across the Web
How to use static routes with mix and Vuejs #154 - GitHub
The command should support your config file. You'll need to import the route command in your Vue components https://github.com/tightenco/ziggy/ ...
Read more >Dynamic routing using Vue Router - LogRocket Blog
In this tutorial, we'll explore dynamic routing in Vue using Vue Router, learning how it can improve our app's performance.
Read more >Routes' Matching Syntax - Vue Router
Routes' Matching Syntax #. Watch a free video lesson on Vue School. Most applications will use static routes like /about and dynamic routes...
Read more >Vuejs : how to mix static and dynamic content in template?
You can use javascript in attribute binding: <a :href="`#fiche?username=${username}`">. Or <a :href="'#fiche?username=' + username">.
Read more >Route Props - A Vue.js Lesson From our Vue.js Course
Setting the route records props property to a function is the most versatile. It allows you to cast parameters into other types, combine...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok, I think I figured it out. First, dumping all the named routes into a publically-available
<script>
tag andwindow.Ziggy
is a leaky abstraction in my opinion. I do not like how implementation details about routes are leaked to public users; however, an alternate solution exists, which is to usewhitelist
andblacklist
. You can read more here: https://github.com/tightenco/ziggyThe solution described in this thread is probably what you want to do with Laravel/Vue.
Here is what I am doing now:
Run
php artisan ziggy:generate "resources/js/ziggy.js"
(this will generate the route list into the same folder as yourapp.js
file, and this is what makesimport { Ziggy } from './ziggy'
work, assuming you import that into your app.js file.Continuing from step 1, add this to your
app.js
file:Your relative folder may vary. If you are using VS Code, I recommend the extension
Path Autocomplete
because it will give you typeahead suggestions while typing the path. I usually do something likeimport route from '../';
and then follow the dropdown suggestions. It makes it easy to see what folder you are in. Here is a screenshot:app.js
:Now you can access
this.route('some.name')
from any Vue component.You will notice that you need to run
php artisan ziggy:generate
every time you changeweb.php
. This is horrifically annoying. I tried in my own app to makelaravel-mix
run that command every time a file changed, but it ended up being very non-trivial.The closest I got was to install
webpack-shell-plugin
and make it so it rebuilt theziggy.js
file, but because I am using Webpack V3 (for dynamic imports and bundle splitting, V4 is too buggy), I was encountering some infinite loops where Webpack was rebuilding every time it rebuilt, due to a change inpublic/css/app.css
which is post-processed by SASS and Tailwind (in my case). I also could not stop it by using BrowserSync’s or Webpack’signore
options.Your milage will vary. Try it using
onBuildEnd
withwebpack-shell-plugin
. If you are using Webpack V4, you can probably useafterEmit
(see: https://stackoverflow.com/questions/30312715/run-command-after-webpack-build).As a stop-gap fix, I have done the following: layout.blade.php
app.js
webpack.config.js
The important thing to note is that I am pushing the shell plugin only in production mode. Webpack did not like when the plugins array contained falsy and/or non-function values because it assumes
fn.call()
andfn.apply()
can be used on every entry.The only other thing to note is that maintaining a difference between production and non-production environments is probably sub-optimal and could cause bugs over the lifespan of your app. If ever your environment flags do not initialize correctly, your routes may malfunction.
@rodrigopedra thanks for the info. This should work for me. I’ll test it out when I get back to working on the project again.
I think some info like this should probably be added to the readme file around generating the Custom Ziggy file.