Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel : How to hide the interpolation code before Vue js loads?

I have a Laravel website, when I use Laravel to load my data from the database, then a pass the results to JavaScript this way

<script>
window.forfaits = <?php echo json_encode($results); ?>;
</script>

Then I use Vue js v-for to display my data. The problem is that I see interpolation on the home page before Vue Js loads and v-cloak can't do the work since I'm getting my data with php then pass to js.

How can I do so that interpolation doesn't show on the page?

UPDATE

By interpolation I mean this:

enter image description here

And here is my main.blade.php file which is loaded as the home page:

<script>
window.forfaits = <?php echo json_encode($forfaits); ?>;
</script>
@extends('layouts.app')

<div>
@section('main-content')
<div class="col-sm-8 col-md-9">
    <div id="filter-items">
        <div class="product-item offre" v-for="forfait in filteredForfaits">
            <div class="product-na">
                <h3 class="product-name">@{{forfait.nom_forfait}}</h3>
                <div class="product-actions">
                 ............................
like image 869
Nacim Idjakirene Avatar asked Oct 18 '17 08:10

Nacim Idjakirene


People also ask

How do you hide an element in Vue?

When using v-show="false" the element is rendered in the DOM, however, Vue applies the inline style display: none that hides the element completely. Also, do not forget about the powerful :class binding if you'd like more visibility customization. To hide the element but keep its space use :class="{ invisible: !

What is V-cloak?

The v-cloak directive is a Vue. js directive that will remain on the element until the associated Vue instance finishes compilation. Combined with CSS rules such as [v-cloak] { display: none }, this directive can be used to hide uncompiled mustache bindings until the Vue instance is ready.

Is a attribute that you can add to a element you want to hide while Vue is mounting soon as Vue is ready this attribute is removed?

v-cloak is a attribute that you can add to a element you want to hide, while Vue is mounting. Soon as Vue is ready, this attribute is removed.


1 Answers

If you're trying to use v-cloak to hide uncompiled templates you should create CSS rule as described here: https://vuejs.org/v2/api/#v-cloak

It works really simple. Uncompiled templates is hidden by v-cloak css rule. If Vue compiler seeing v-cloak attribute on the compiled template, it simply deletes the attribute and your component appears on the page.

[v-cloak] {
  display: none;
}

Have you include it?

Also, you must be sure that css file, contains [v-cloak] rule will be loaded at a time the uncompiled Vue templates appears. You should decide critical css or use rel="preload" (already available in modern browsers) for that piece of css.

like image 146
pinguinjkeke Avatar answered Oct 04 '22 21:10

pinguinjkeke