@vite( 'resources/css/app.css' 'resources/js/app.js' ) is not working with laravel 9, vite 4.0.0

huangapple go评论65阅读模式
英文:

@vite( 'resources/css/app.css' 'resources/js/app.js' ) is not working with laravel 9, vite 4.0.0

问题

在Laravel 9应用程序中,我使用以下命令安装了Laravel auth(带有Jetstream Livewire和vite-4.0.0):

composer require laravel/jetstream

php artisan jetstream:install livewire

php artisan migrate
npm install
npm run dev
npm run build

在我的app.blade.phpguest.blade.php中,@vite('resources/css/app.css' 'resources/js/app.js') 不起作用。CSS和JS没有加载。

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">
        <meta name="csrf-token" content="{{ csrf_token() }}">

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

        <!-- Fonts -->
        <link rel="preconnect" href="https://fonts.bunny.net">
        <link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet" />

        <!-- Scripts -->
        @vite(['resources/css/app.css', 'resources/js/app.js'])

        <!-- Styles -->
        @livewireStyles
    </head>

    <body>
        <div class="font-sans text-gray-900 antialiased">
            {{ $slot }}
        </div>
        @livewireScripts
    </body>
</html>

我已尝试此问题的解决方案,但没有效果。

英文:

In Laravel 9 app, I installed Laravel auth (with Jetstreem livewire & vite-4.0.0) using the following commands:

composer require laravel/jetstream

php artisan jetstream:install livewire

php artisan migrate
npm install
npm run dev
npm run build

In my app.blade.php and guest.blade.php @vite( 'resources/css/app.css' 'resources/js/app.js' ) is not working. CSS & JS is not loading.

app.blade.php

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;{{ str_replace(&#39;_&#39;, &#39;-&#39;, app()-&gt;getLocale()) }}&quot;&gt;
    &lt;head&gt;
        &lt;meta charset=&quot;utf-8&quot;&gt;
        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
        &lt;meta name=&quot;csrf-token&quot; content=&quot;{{ csrf_token() }}&quot;&gt;

        &lt;title&gt;{{ config(&#39;app.name&#39;, &#39;Laravel&#39;) }}&lt;/title&gt;

        &lt;!-- Fonts --&gt;
        &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.bunny.net&quot;&gt;
        &lt;link href=&quot;https://fonts.bunny.net/css?family=figtree:400,500,600&amp;display=swap&quot; rel=&quot;stylesheet&quot; /&gt;

        &lt;!-- Scripts --&gt;
        @vite([&#39;resources/css/app.css&#39;, &#39;resources/js/app.js&#39;])

        &lt;!-- Styles --&gt;
        @livewireStyles
    &lt;/head&gt;

    &lt;body&gt;
	&lt;div class=&quot;font-sans text-gray-900 antialiased&quot;&gt;
		{{ $slot }}
	&lt;/div&gt;
	@livewireScripts
    &lt;/body&gt;

&lt;/html&gt;

I have tried the solutions of This Question but nothing worked.

答案1

得分: 0

你是否也将它们添加到了 vite.config.js 中,并设置了相应的路径?

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig(
{
    plugins: [
        laravel({
            input: [
                'resources/admin/scss/style.scss',
                'resources/css/app.css',
                'resources/js/app.js',
            ],
            refresh: true,
        }),
    ],
});
英文:

Did you added them too into vite.config.js with corresponding path

import { defineConfig } from &#39;vite&#39;;
import laravel from &#39;laravel-vite-plugin&#39;;

export default defineConfig(
{
    plugins: [
        laravel({
            input: [
                &#39;resources/admin/scss/style.scss&#39;,
                &#39;resources/css/app.css&#39;,
                &#39;resources/js/app.js&#39;,
            ],
            refresh: true,
        }),
    ],
});

</details>



# 答案2
**得分**: 0

以下是翻译好的部分:

问题出在 **app.css** 上。 Vite 配置在没有 CSS 的情况下运行得很好。

这是解决方案:

1. **vite.config.js**
---------------------

从 **vite.config.js** 中移除 `&#39;resources/js/app.css&#39;`



```javascript
import { defineConfig } from &#39;vite&#39;;
import laravel, { refreshPaths } from &#39;laravel-vite-plugin&#39;;

export default defineConfig({
    plugins: [
        laravel({
            input: [
                &#39;resources/js/app.js&#39;,
            ],
            refresh: [
                ...refreshPaths,
                &#39;app/Http/Livewire/**&#39;,
            ],
        }),
    ],
});
  1. resources/js/app.js 中添加 CSS

import &#39;../css/app.css&#39;;

像这样:

import &#39;./bootstrap&#39;
import &#39;../css/app.css&#39;;

import Alpine from &#39;alpinejs&#39;;
import focus from &#39;@alpinejs/focus&#39;;
window.Alpine = Alpine;

Alpine.plugin(focus);

Alpine.start();
  1. 在视图中使用 @vite

因为我们的 CSS 已经被导入到我们的 app.js 中,所以我们只需要在我们的布局视图文件 app.blade.phpguest.blade.php 中使用

@vite(&#39;resources/js/app.js&#39;)
<!DOCTYPE html>
<html lang="{{ str_replace(&#39;_&#39;, &#39;-&#39;, 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(&#39;app.name&#39;, &#39;Laravel&#39;) }}</title>

    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.bunny.net">
    <link href="https://fonts.bunny.net/css?family=figtree:400,500,600&amp;display=swap" rel="stylesheet" />

    <!-- Styles -->
    @livewireStyles
    @vite(&#39;resources/js/app.js&#39;)
</head>

<body>
    <div class="font-sans text-gray-900 antialiased">
        {{ $slot }}
    </div>
</body>

</html>
  1. 构建您的应用程序

运行 npm run build

注意:

如果需要,使用 php optimize:clear 进行缓存清理。

英文:

The problem is with app.css. Vite config works great without CSS.

Here's the solution:

  1. vite.config.js

Remove &#39;resources/js/app.css&#39; from vite.config.js

import { defineConfig } from &#39;vite&#39;;
import laravel, { refreshPaths } from &#39;laravel-vite-plugin&#39;;

export default defineConfig({
    plugins: [
        laravel({
            input: [
                &#39;resources/js/app.js&#39;,
            ],
            refresh: [
                ...refreshPaths,
                &#39;app/Http/Livewire/**&#39;,
            ],
        }),
    ],
});
  1. Add CSS to resources/js/app.js

`import &#39;../css/app.css&#39;;`

Like

import &#39;./bootstrap&#39;;
import &#39;../css/app.css&#39;;

import Alpine from &#39;alpinejs&#39;;
import focus from &#39;@alpinejs/focus&#39;;
window.Alpine = Alpine;

Alpine.plugin(focus);

Alpine.start();
  1. Using @vite in views

Because our CSS is already imported into our app.js, we will only use

@vite(&#39;resources/js/app.js&#39;)

in our layout view files: app.blade.php and guest.blade.php

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;{{ str_replace(&#39;_&#39;, &#39;-&#39;, app()-&gt;getLocale()) }}&quot;&gt;

&lt;head&gt;
	&lt;meta charset=&quot;utf-8&quot;&gt;
	&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
	&lt;meta name=&quot;csrf-token&quot; content=&quot;{{ csrf_token() }}&quot;&gt;

	&lt;title&gt;{{ config(&#39;app.name&#39;, &#39;Laravel&#39;) }}&lt;/title&gt;

	&lt;!-- Fonts --&gt;
	&lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.bunny.net&quot;&gt;
	&lt;link href=&quot;https://fonts.bunny.net/css?family=figtree:400,500,600&amp;display=swap&quot; rel=&quot;stylesheet&quot; /&gt;

	&lt;!-- Styles --&gt;
	@livewireStyles
	@vite(&#39;resources/js/app.js&#39;)
&lt;/head&gt;

&lt;body&gt;
	&lt;div class=&quot;font-sans text-gray-900 antialiased&quot;&gt;
		{{ $slot }}
	&lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;
  1. Build your app

npm run build

Note:

Use php optimize:clear for cache cleaning, if needed.

huangapple
  • 本文由 发表于 2023年6月22日 16:12:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定