Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to escape curly braces in vue.js

I have data in my database that may contains curly braces{{ }}.

{{-- inside app.blade.php --}}
<!-- vue app -->
<div id="app">
    ...code
    <div> {{ $data }} </div>
    ...code
</div>

so if I want to display it to the user this data cause problem if it's inside Vue app. and vue think it's javascript codes to execute.

for example if the $data is equal to {{ title->links() }} then I get an error and the whole app doesn't compile at all. (it passes the blade template).

[Vue warn]: Error compiling template:

invalid expression: expected expression, got '>' in

    _s(title->links())

  Raw expression: {{ title->links() }}

305|              <div>{{ title-&gt;links() }}</div>
   |                   ^^^^^^^^^^^^^^^^^^^^^^^

what is the best way to escape {{ }} curly braces for user data (in Vue.js)??

like image 380
Mohammad Hossein Avatar asked Jan 25 '23 19:01

Mohammad Hossein


1 Answers

You need use the v-pre or v-html directive:

<div v-pre>{{ data }}</div>

or

<div v-html="'{{ data }}'"></div>

ref link https://v2.vuejs.org/v2/api/#v-pre

like image 54
Kamlesh Paul Avatar answered Mar 06 '23 21:03

Kamlesh Paul