简体   繁体   English

Laravel:如何在 Vue js 加载之前隐藏插值代码?

[英]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我有一个 Laravel 网站,当我使用 Laravel 从数据库中加载我的数据时,然后以这种方式将结果传递给 JavaScript

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

Then I use Vue js v-for to display my data.然后我使用 Vue js v-for来显示我的数据。 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.问题是我在加载 Vue Js 之前在主页上看到插值,并且 v-cloak 无法完成工作,因为我使用 php 获取数据然后传递给 js。

How can I do so that interpolation doesn't show on the page?我怎样才能使插值不显示在页面上?

UPDATE更新

By interpolation I mean this:通过插值,我的意思是:

在此处输入图像描述

And here is my main.blade.php file which is loaded as the home page:这是我作为主页加载的 main.blade.php 文件:

<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">
                 ............................

If you're trying to use v-cloak to hide uncompiled templates you should create CSS rule as described here: https://v2.vuejs.org/v2/api/#v-cloak如果您尝试使用v-cloak隐藏未编译的模板,您应该按照此处所述创建 CSS 规则: https ://v2.vuejs.org/v2/api/#v-cloak

It works really simple.它的工作原理非常简单。 Uncompiled templates is hidden by v-cloak css rule.未编译的模板被v-cloak css 规则隐藏。 If Vue compiler seeing v-cloak attribute on the compiled template, it simply deletes the attribute and your component appears on the page.如果 Vue 编译器在编译的模板上看到v-cloak属性,它会简单地删除该属性,并且您的组件会出现在页面上。

[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.此外,您必须确保在出现未编译的 Vue 模板时加载包含 [v-cloak] 规则的 css 文件。 You should decide critical css or use rel="preload" (already available in modern browsers) for that piece of css.您应该决定关键的 css 或使用rel="preload" (在现代浏览器中已经可用)那块 css。

First of all, are you loading Vue from the bottom of your HTML code?首先,您是从 HTML 代码的底部加载 Vue 吗? if you do maybe you should put it inside your head tag so it could be loaded earlier.如果你这样做,也许你应该把它放在你的 head 标签中,这样它就可以更早地加载。

But, my advice for you is to build your whole app thourgh Vue, i can see that you're building a panel of something?但是,我对您的建议是通过 Vue 构建您的整个应用程序,我可以看到您正在构建一个面板? Maybe you should consider building everything with Vue and get the data with an API endpoint that Laravel will provide you, that's how I'd build it.也许你应该考虑使用 Vue 构建所有东西,并使用 Laravel 提供的 API 端点获取数据,这就是我构建它的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM