【问题标题】:laracasts doesn't work (ReferenceError: foo is not defined)laracasts 不起作用(ReferenceError: foo is not defined)
【发布时间】:2021-05-13 04:56:54
【问题描述】:

我正在使用 laravel6 和 laracasts https://github.com/laracasts/PHP-Vars-To-Js-Transformer

我的控制器看起来像这样。

public function index()
    {
        $posts = Post::where('user_id', '<>', \Auth::id())->orderBy('created_at', 'desc')->paginate(3);
        \JavaScript::put([
            'foo' => 'bar',
        ]);
        return view('posts/index', ['posts' => $posts]);
    }

这是我的index.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ config('app.name')}}</title>
    <link rel="stylesheet" href="{{ asset('/css/main.css') }}">
</head>

<body>
    <header>
        <div class="container">
            <a class="brand" href="/">{{ config('app.name') }}</a>
            @include('commons/nav')
        </div>
    </header>

    <main>
        <script>
            console.log(foo);
        </script>
        
        <div class="container2">
            @yield('content')
        </div>
    </main>
</body>
</html>

当我运行时,我在浏览器控制台上得到了Uncaught ReferenceError: foo is not defined。 我想我确实将示例代码复制并粘贴到https://github.com/laracasts/PHP-Vars-To-Js-Transformer

谁能告诉我怎么了?谢谢。

【问题讨论】:

    标签: javascript php html laravel-6


    【解决方案1】:

    您在正文底部上方的index.blade.php 文件中缺少@include ('footer') 调用。

    https://github.com/laracasts/PHP-Vars-To-Js-Transformer

    【讨论】:

    • 谢谢!但是,这并不能解决问题。我改变了config/javascript.php 这样'bind_js_vars_to_this_view' =&gt; ['posts/index'] 然后,如果固定的话。
    猜你喜欢
    • 1970-01-01
    • 2014-03-03
    • 2021-09-18
    • 2020-12-26
    • 2018-12-26
    • 2020-06-14
    • 2019-02-08
    • 2015-11-19
    • 2019-11-10
    相关资源
    最近更新 更多