Anil Kumar Sahu
Anil Kumar Sahu

Reputation: 577

How to change delimiters in Vue.js?

I am working on a project in Laravel and Vue.js. When I use Vue.js syntax like {{var_name}}, I get an error because the Laravel delimiter is also {{ }}. How can I change the delimiter for Vue.js so that it doesn't conflict with Laravel?

Here is the code I am using for layout.app.blade.php:

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'Laravel') }}</title>

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
     <script src="{{ asset('js/vue.min.js') }}"></script>
  <link rel="stylesheet" href="{{asset('js/style.css') }}">
</head>
<body>
    <div id="app">
        <nav class="navbar navbar-default navbar-static-top">
            <div class="container">
                <div class="navbar-header">

                    <!-- Collapsed Hamburger -->
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
                        <span class="sr-only">Toggle Navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>

                    <!-- Branding Image -->
                    <a class="navbar-brand" href="{{ url('/') }}">
                        {{ config('app.name', 'Laravel') }}
                    </a>
                </div>

                <div class="collapse navbar-collapse" id="app-navbar-collapse">
                    <!-- Left Side Of Navbar -->
                    <ul class="nav navbar-nav">
                        &nbsp;
                    </ul>

                    <!-- Right Side Of Navbar -->
                    <ul class="nav navbar-nav navbar-right">
                        <!-- Authentication Links -->
                        @if (Auth::guest())
                            <li><a href="{{ route('login') }}">Login</a></li>
                            <li><a href="{{ route('register') }}">Register</a></li>
                        @else
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                                    {{ Auth::user()->name }} <span class="caret"></span>
                                </a>

                                <ul class="dropdown-menu" role="menu">
                                    <li>
                                        <a href="{{ route('logout') }}"
                                            onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                                            Logout
                                        </a>

                                        <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                            {{ csrf_field() }}
                                        </form>
                                    </li>
                                </ul>
                            </li>
                        @endif
                    </ul>
                </div>
            </div>
        </nav>

        @yield('content')
    </div>

    <!-- Scripts -->
    
<script src="{{ asset('js/grid/grid.js') }}"></script>
    
</body>
</html>

And the code for my homepage:

@extends('layouts.app')

@section('content')
 <link rel="stylesheet" href="{{asset('js/style.css')}}">
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Dashboard</div>

                <div class="panel-body">
                    @if (session('status'))
                        <div class="alert alert-success">
                            {{ session('status') }}
                        </div>
                    @endif

                   <div id="app">
                    <div class="container col-lg-offset-4 col-lg-4">
                        <br>

                            <div class="row">
                                


 <!-- component template -->
    <script type="text/x-template" id="grid-template">
      <table v-if="filteredData.length">
        <thead>
          <tr>
            <th v-for="key in columns"
              @click="sortBy(key)"
              :class="{ active: sortKey == key }">
              {{ key | capitalize }}
              <span class="arrow" :class="sortOrders[key] > 0 ? 'asc' : 'dsc'">
              </span>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="entry in filteredData">
            <td v-for="key in columns">
              {{entry[key]}}
            </td>
          </tr>
        </tbody>
      </table>
      <p v-else>No matches found.</p>
    </script>

    <!-- demo root element -->
    <div id="demo">
      <form id="search">
        Search <input name="query" v-model="searchQuery">
      </form>
      <demo-grid
        :data="gridData"
        :columns="gridColumns"
        :filter-key="searchQuery">
      </demo-grid>
    </div>

















                            </div>

                    </div>
    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

<script src="{{ asset('js/grid/grid.js') }}"></script>

Upvotes: 12

Views: 14410

Answers (4)

Niklesh Raut
Niklesh Raut

Reputation: 34914

To change delimiters in vue.js, use like this

new Vue({
    delimiters: ['{%', '%}'],
}

Explode more about delimiters in vuejs docs

var data = {
    message: 'Hello Vue.js!'
}

var demo = new Vue({
    el: '#demo',
    delimiters: ['{%', '%}'],
    data: data
});
<script src="https://vuejs.org/js/vue.min.js"></script>
<div id="demo">
    <p> {% message %} </p>
    <input v-model="message">
</div>

Upvotes: 18

Phil
Phil

Reputation: 1219

TLDR: It seems since Vue 3.1 old delimiter is deprecated and I was not able to have this option inlined in the source. My goal is to use Vue 3 with Flask. I've set up options.compilerOptions in vue.config.js.

I copy my answer also on a github closed issue : vuejs/vue-next #1679 : Facility to change template mustaches in Vue3, delimiters no longer works

Got some good rowing since 2 days. :))

I've read below again and again https://v3.vuejs.org/api/application-config.html#compileroptions

Important This config option is only respected when using the full build (..) For vue-loader: pass via the compilerOptions loader option (opens new window). Also see how to configure it in vue-cli (opens new window).

After that I read on Stackoverflow

Before I (re)installed below those Vue 3 package only if I've done mistake by copy paste from stackoverflow. I assume they are yet installed.

npm i --save vue@next (I assume Vue 3 is yet installed (vue@next), it's only if I copy paste vue@latest by mistake
npm install -g @vue/cli) 

And I installed for Vue 3:

npm i --save vue-loader@next
npm i --save vue-template-compiler@latest 
nom i --save vue-server-renderer@latest

I created vue.config.js below

module.exports = {
  chainWebpack: config => {
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions = {
          delimiters: ['${', '}']
        };
        return options;
      });
  }
}

With this App.vue from the vue create with my modifications :

<template>
   <img alt="Vue logo" src="./assets/logo.png">
   <div id="list-rendering" class="demo">
      <ol>
         <li v-for="todo in todos" v-bind:key="todo" >
            new delimiter for flask:  ${todo.text} <br>
            initial vue delimiter:  {{todo.text}} 
         </li>
      </ol>
   </div>
</template>
<script>
   export default {
     name: 'App',
   
   data() {
       return {
         todos: [
           { text: 'Learn JavaScript' },
           { text: 'Learn Vue' },
           { text: 'Build something awesome' }
         ]
       } 
     }
   }
</script>
<style>
   #app {
   font-family: Avenir, Helvetica, Arial, sans-serif;
   -webkit-font-smoothing: antialiased;
   -moz-osx-font-smoothing: grayscale;
   text-align: center;
   color: #2c3e50;
   margin-top: 60px;
   }
</style>

Upvotes: 0

Niclas von Caprivi
Niclas von Caprivi

Reputation: 769

If you are working with Vue 3.x, you can change delimiters according to the documentation:

Vue.createApp({
  // Delimiters changed to ES6 template string style
  delimiters: ['${', '}']
})

Upvotes: 5

common sense
common sense

Reputation: 3912

The recommended way to use VueJS inside Blade is to append an @ sign at VueJS delimiters: @{{ variable }}.

Upvotes: 3

Related Questions