如何修复 app.js 和 Laravel 中的 app.css 错误 404?

How to fix error 404 with app.js and app.css in Laravel?

我正在使用 Laravel 6 和 Bootstrap 4.

我制作了一个小应用程序,我试图更改 CSS 文件中 body 的背景颜色,但我发现它不起作用。

所以我检查了开发人员的工具,发现有两个关于app.jscss/app的错误。 css

"Failed to load resource: the server responded with a status of 404 (Not Found)"

我尝试在 CSS 的路径前添加 "/" 但我失败了。

查看:

<!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() }}">

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

    <!-- Styles -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css">


    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

</head>
<body style="overflow: hidden; ">
    <div id="app">
        @include('../inc/navbar')
        <main class="py-4">
            @yield('content')
        </main>
        <br>
        <br>
        <br>
        @include('../inc/footer')
    </div>
    </body>
</html>

CSS:

body {
    background-color: lightblue !important;
}

我想看看浅蓝色 body 但由于某些原因它不起作用。你有什么想法吗?

I was trying to change the background color of the body in the css file but I noticed that it didn't work.

如果这是 Laravel 的全新安装,您需要 运行 npm install。安装所有前端资产(css、js 等)。如果您 从未 对 css 或 js 进行过任何更改,则在终端中您需要 运行 此命令:npm run devnpm run watchHere is more documentation 为 Laravel 混合。

您不会想要触摸实际的 .css 文件;每当资产编译时,它都会被覆盖。使用上述命令会将所有 .scss 文件重新编译为 .css.

希望对您有所帮助!

您需要编译资产。 (假设你已经安装了 npm)

运行

npm run prod - if production 

npm run dev  - if development

您还可以在编译资产之前更改文件 resources/sass/_variables.scss 中的颜色。

此错误表示 app.jsapp.css 的位置不正确:

"Failed to load resource: the server responded with a status of 404 (Not Found)"

解法:

让你的JS目录结构如下:/public/js/app.js

CSS的目录结构如下:/public/css/app.css