question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation is missing the requirement of `vendor/tightenco/ziggy/src/js/route.js`

See original GitHub issue

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!)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:21 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
markokeeffecommented, Sep 20, 2019

When using Vue.mixin(), you are essentially adding something to every component definition, so the following snippet:

Vue.mixin({
    methods: {
        route: (name, params, absolute) => route(name, params, absolute, Ziggy),
    }
});

adds a route() method to every component (accessible with this.route('route.name.here').

For your example, you don’t need to assign route at all in the data() { ... } portion of your component. You can fix your importUser() method by simply using axios.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:

// resources/js/route.js
import route from 'ziggy'
import { Ziggy } from './ziggy'

export default function (name, params, absolute) {
    return route(name, params, absolute, Ziggy)
}

Then you can import it into app.js to set up the global mixin:

// resources/js/app.js
import route from './route'

Vue.mixin({
    methods: {
        route,
    },
})

Or in any other module / js file e.g. a VueX store:

// resources/js/store/mymodule.js
import axios from 'axios'
import route from '../route'

// actions
const actions = {
    getExample({ state }) {
        return axios.get(route('my-route'))
    },
}
2reactions
markokeeffecommented, Sep 19, 2019

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) uses resources/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:

laravel new ziggy-laravel-test
cd ziggy-laravel-test
composer require laravel/ui --dev
composer require tightenco/ziggy
php artisan ui vue
yarn install

webpack.mix.js

const mix = require('laravel-mix');
const path = require('path');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.webpackConfig({
    resolve: {
        alias: {
            ziggy: path.resolve('vendor/tightenco/ziggy/src/js/route.js'),
        },
    },
});

mix.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css');

routes/web.php

<?php
Route::get('/', function () {
    return view('welcome');
})->name('welcome'); // Need at least one named route

Run Ziggy

php artisan ziggy:generate ./resources/js/ziggy.js

resources/js/app.js

import route from 'ziggy';
import { Ziggy } from './ziggy';

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

Vue.mixin({
    methods: {
        route: (name, params, absolute) => route(name, params, absolute, Ziggy),
    }
});

/**
 * The following block of code may be used to automatically register your
 * Vue components. It will recursively scan this directory for the Vue
 * components and automatically register them with their "basename".
 *
 * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
 */

// const files = require.context('./', true, /\.vue$/i)
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))

Vue.component('example-component', require('./components/ExampleComponent.vue').default);

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

const app = new Vue({
    el: '#app',
});

** resources/js/components/ExampleComponent.vue **

<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Example Component</div>

                    <div class="card-body">
                        <!-- Using ziggy route helper here -->
                        <a :href="route('welcome')" class="btn">Go Home</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>

Add built assets to your blade layout

<head>
    ...
   <script src="{{ mix('/js/app.js') }}" defer></script>
    <link href="{{ mix('/css/app.css') }}" rel="stylesheet">
</head>

Add Vue component to your blade view

<div id="app" class="content">
    <example-component/>
</div>

Build assets

yarn run dev
Read more comments on GitHub >

github_iconTop Results From Across the Web

Type 'Document' is missing the following properties from type
The problem is I am having this typescript error that says "Type 'Document' is missing the following properties from type 'SavedUser': firstName ...
Read more >
[BUG] Missing required argument #1 #681 - npm/cli - GitHub
My solution works for me ! Install node.js 及 npm. sudo apt install nodejs sudo apt install npm.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found