What is the best way to use Laravel route in Vue JS component What is the best way to use Laravel route in Vue JS component vue.js vue.js

What is the best way to use Laravel route in Vue JS component


You can pass the route as props to the component inside the blade file.

Inside the view:

<component home-route="{{ route('home') }}"></component>

Inside the vue component:

<script>     export default {        props: ['homeRoute'],    }</script>

Then you can access the route inside the component like so:

this.homeRoute

You can learn more about props here: https://vuejs.org/v2/guide/components-props.html

Hope this helps.


The only way how to use routes with parameters that I found out is like this:

The route:

Route::get('route/{param1}/{param2}', [Controller::class, 'actionName'])->name('routeName');

The blade:

<component :route="'{{ route('routeName', ["", ""]) }}'"></component>

where the number of empty strings in the array is equal to the number of required parameters for the route.

The component:

<script>export default {    props: {        route: String    },    data() {        return {            param1: "",            param2: ""        }    },    computed: {        fullRoute() {            return this.route + '/' + this.param1 + '/' + this.param2;        }    }}</script>

I am using Laravel 8 and Vue 3.