当我使用 laravel/ui 时,插入的脚本不起作用

我正在使用 Node js 中的模板(我只是使用它,因为我正在学习某些课程,这是步骤之一)。考虑到我在不同帖子中读到的内容,bootstrap 使用 jquery。然后,我意识到当我在其他视图中插入脚本时,head 中的脚本会造成麻烦。


例如。这是我的 app.blade.php,在头部,我将脚本作为注释。


<!doctype html>

<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>

    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1">


    <!-- CSRF Token -->

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

    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">   

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


    <!-- Scripts 

    <script src="{{ asset('js/app.js') }}" defer></script>}-->


    <!-- Fonts -->

    <link rel="dns-prefetch" href="//fonts.gstatic.com">

    <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">

    

    <!-- Bootstrap css -->

    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">

    <!-- Style css -->

    <link rel="stylesheet" type="text/css" href="css/style.css">

    <!-- Styles -->

    <link href="{{ asset('css/app.css') }}" rel="stylesheet">

    <link rel="stylesheet" type="text/css" href="css/style.css">

</head>

我一直遇到麻烦的观点是这个


@extends('layouts.app')

@section('grid')

    <!-- Bootstrap css -->

    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">

    <!-- Style css -->

    <link rel="stylesheet" type="text/css" href="css/style.css">

@endsection


@section('content')

    <div class="container">

        <h1 class="text-center">NUESTROS PRODUCTOS</h1>

        <hr>


问题是,如果我让 app.blade.php 标签脚本脱离注释,我的目标视图中的脚本(最后一个代码)将不再工作。


对于我的子视图,我想在数据库中插入一些数据而不返回视图。我根本没有收到任何错误生成。这听起来很简单,如果我不在我的 app.blade.php 头中注释脚本,那么我的子视图中的脚本可以帮助我避免在我的数据库中插入数据时返回视图,但它不起作用,我可以通过一个简单的警报来更改最后一个,它不会返回任何内容,而是返回一个空白视图。


如果您知道我应该在文档中查看什么样的信息,我将不胜感激。


跃然一笑
浏览 69回答 1
1回答

慕雪6442864

我终于修复了它,它似乎与我在子视图中插入脚本代码的方式有问题。我必须阅读有关堆栈的内容(检查https://laravel.com/docs/7.x/blade#stacks),这似乎是在父视图中插入脚本的正确方法。父视图与问题中发布的相同,但我取消了头部脚本的注释,并将其放在关闭正文标签之前:@stack('script')我的孩子们的视图现在看起来像这样(重点关注push和endpush部分,这是有问题的脚本):@extends('layouts.app')@section('grid')<!-- Bootstrap css --><link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"><!-- Style css --><link rel="stylesheet" type="text/css" href="css/style.css">@endsection@push('script')<script>&nbsp; &nbsp; x = document.getElementById("alertabien");&nbsp; &nbsp; y = document.getElementById("alertamal");&nbsp; &nbsp; x.style.display =&nbsp; "none";&nbsp; &nbsp; y.style.display =&nbsp; "none";@foreach($productos as $product)&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('postForm{{$product->id}}').addEventListener('submit', prueba{{$product->id}});&nbsp; &nbsp; &nbsp; &nbsp; function prueba{{$product->id}}(e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var id = document.getElementById({{$product->id}}).value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var params = "idproducto="+id+"&idcarrito="+{{$carrito_id[0]->id}};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhr = new XMLHttpRequest();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhr.open('POST', '/carrito', true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhr.setRequestHeader('Content-type', "application/x-www-form-urlencoded");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhr.setRequestHeader('X-CSRF-TOKEN', "{{ csrf_token() }}");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhr.onload = function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(this.responseText){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("alertamal").innerHTML = "Lo sentimos, ha habido un error en la base de datos, por favor recargue la página.";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y.style.display =&nbsp; "block";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#alertamal").fadeIn("slow");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#alertamal").fadeOut(8000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("alertabien").innerHTML = "¡FELICIDADES! Ha añadido correctamente su producto al carrito de compras";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.style.display =&nbsp; "block";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#alertabien").fadeIn("slow");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#alertabien").fadeOut(8000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xhr.send(params);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;@endforeach</script>@endpush@section('content')<div class="alert alert-success" id="alertabien"></div><div class="alert alert-danger" id="alertamal"></div><div class="container">&nbsp; &nbsp; <h1 class="text-center">NUESTROS PRODUCTOS</h1>&nbsp; &nbsp; <hr>&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; @foreach ($productos as $producto)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-4 product-grid">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="image">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="storage/app/images/apple-watch.jpg" class="w-100">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="overlay">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="detail">View Details</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form id="postForm{{$producto->id}}" method="POST" class="postForm">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @csrf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" id="{{$producto->id}}" name="producto_id" class="producto_id" value="{{$producto->id}}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5 class="text-center">{{$producto->nombre}}</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5 class="text-center">Price: {{$producto->precio}}</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input id="guarda" name="guarda"type="submit" value="AÑADIR AL CARRITO" class="btn buy btn-enviar">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- /Product -->&nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; </div></div>@endsection
打开App,查看更多内容
随时随地看视频慕课网APP