How can I use Blade in Vue files?
Asked Answered
S

4

5

I'm trying to use Blade in my Vue file carousel-signUp.vue

Like:

<template>
    <div>           
        <form action="{{route('dump')}}" method="POST" >
           /* Some code here...*/
        </form>
   </div>
</template>

But in the end I'm getting an error.

The compiler can't understand the difference between Vue syntax and Blade syntax. How can I define Blade syntax in Vue files?

Or, does anyone have an idea how to use {{route('dump')}} value in my vue file?

Spit answered 18/7, 2017 at 10:4 Comment(2)
Blade cannot be used in vue files.Orbadiah
have any idea for using {{route('dump')}} value in my vue file ?? :(Spit
R
5

A better solution might be to output the desired blade variables to (hidden) html and select that html in your Vue code. Escape the curly braces for Vue with @.

Your blade example.blade.php

<div id="content" hidden>
    {{ bladeVar }}
</div>

<div id="app">
    @{{ vueVar }}
</div>

The result of that blade (how it will look in the cached file) + javascript (Vue)

var app = new Vue({
    el: '#app',
    data: {
        vueVar: document.getElementById('content').innerHTML + 'from Vue'
    }
});
<script src="https://unpkg.com/vue"></script>
<div id="content" hidden>
    Hello world
</div>

<div id="app">
    {{ vueVar }}
</div>

I found a post having the same issue as you

Rosenda answered 18/7, 2017 at 11:23 Comment(0)
J
3

You can't use blade in your Vue files, but you can use Vue slots so you can send the content from your blade file to the component:

Vue file

<template>
    <div>           
        <slot></slot>
    </div>
</template>

Blade

<carousel-sign-up>
    <form action="{{route('dump')}}" method="POST" >
        /* Some code here...*/
    </form>
</carousel-sign-up>

This will be useful when your components should have dynamic content. If in that component you only will need that route from blade and the form will be ever the same, look at @Mick answer.

Jenninejennings answered 18/7, 2017 at 14:39 Comment(0)
G
0

You could pass the "{{route('dump')}}" as a prop to the component.

<component route="/yourRouteHere"> </component>

https://v2.vuejs.org/v2/guide/components.html#Props

Mick

Graybill answered 18/7, 2017 at 11:12 Comment(0)
I
0

You can parse your data to Json and then you can redirect your blade to vue Companent. I will mention below about it.

<your-companent data={{$yourDataFromBackEnd}}></your-companent>

Like that, you can catch as "data" in Vue if you have experience on VueJs.

Iq answered 29/6, 2021 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.