Javascript Laravel app.js延迟未在内容之前加载

Javascript Laravel app.js延迟未在内容之前加载,javascript,laravel,node-modules,Javascript,Laravel,Node Modules,我正在尝试将jquery和use添加到一个新项目中 app.js require('./custom'); require('./bootstrap'); require('alpinejs'); custom.js window.$ = require('jquery'); window.Swal = require('sweetalert2'); guest.blade.php <!DOCTYPE html> <html lang="{{ str_replac

我正在尝试将jquery和use添加到一个新项目中

app.js

require('./custom');
require('./bootstrap');
require('alpinejs');
custom.js

window.$ = require('jquery');
window.Swal = require('sweetalert2');
guest.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">
        <meta name="csrf-token" content="{{ csrf_token() }}">

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

        <!-- Fonts -->
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap">

        <!-- Styles -->
        <link rel="stylesheet" href="{{ mix('css/app.css') }}">

        <!-- Scripts -->
        <script src="{{ mix('js/app.js') }}" defer></script>
    </head>
    <body>
        <div class="font-sans text-gray-900 antialiased">
            {{ $slot }}
        </div>
    </body>
</html>
<x-guest-layout>
    <script>
        function alerta() {//This Work
            Swal.fire("Hello World Sweetalert2");//This Work
            $('.lol').hide();//This Work
        }

        $(function() {//$ This not Work
            console.log( "ready!" );
        });
        Swal.fire("Hello World Sweetalert2");//This not Work
    </script>
    <x-jet-button onclick="alerta()" class="lol">Alert</x-jet-button>
</x-guest-layout>

{{config('app.name','Laravel')}
{{$slot}
欢迎使用.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">
        <meta name="csrf-token" content="{{ csrf_token() }}">

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

        <!-- Fonts -->
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap">

        <!-- Styles -->
        <link rel="stylesheet" href="{{ mix('css/app.css') }}">

        <!-- Scripts -->
        <script src="{{ mix('js/app.js') }}" defer></script>
    </head>
    <body>
        <div class="font-sans text-gray-900 antialiased">
            {{ $slot }}
        </div>
    </body>
</html>
<x-guest-layout>
    <script>
        function alerta() {//This Work
            Swal.fire("Hello World Sweetalert2");//This Work
            $('.lol').hide();//This Work
        }

        $(function() {//$ This not Work
            console.log( "ready!" );
        });
        Swal.fire("Hello World Sweetalert2");//This not Work
    </script>
    <x-jet-button onclick="alerta()" class="lol">Alert</x-jet-button>
</x-guest-layout>

函数alerta(){//这项工作
Swal.fire(“Hello World Sweetalert2”);//这项工作
$('.lol').hide();//这项工作
}
$(函数(){/$这不起作用
console.log(“准备就绪!”);
});
Swal.fire(“Hello World Sweetalert2”)//这不管用
警觉的
最后得到错误uncaughtreferenceerror:$未定义或uncaughtreferenceerror:Swal未定义

我怎样才能解决这个问题

编辑:
多亏了@Areg,这个问题才得以解决,在阅读了

之后,我对这一点有了更好的理解。当你将一些东西写入app.js时,你需要稍后使用
npm run dev
npm run watch
对其进行编译。另外,不要延迟您的应用程序js文件,因为它被设计成在W3K上加载文档后运行脚本

延迟属性是一个布尔属性。

当存在时,它指定在页面完成解析时执行脚本。


注意:defer属性仅适用于外部脚本(仅当src属性存在时才应使用)。

能否共享package.json文件在加载
defer
ed应用程序脚本之前,在welcome.blade.php中加载脚本标记。尝试仅在js中使用jsfiles@NICO如果他想让脚本在页面之前运行,为什么要使用defer?@NICO您没有完全阅读它,实际上缺少两点。而且,如果他将删除延迟,它仍然会起作用,因为正如他指出的那样,
welcome.blade.php是在延迟的应用程序加载之前加载的
@NICO这正是我要告诉你的,这就是为什么你需要删除延迟谢谢,它起作用了!读了以后我更明白了,谢谢你的帮助+1@JhordyBarrera请注意选择它作为正确答案,以防其他人也发现了这一点