laravel 5- css 和 js 没有加载 URL::assets

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44740960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:12:34  来源:igfitidea点击:

laravel 5- css and js are not loading with URL::assets

phplaravellaravel-5

提问by user450_user

I am using laravel 5.2 and I have problem with the assets url , I do not manage to call css to or js links

我正在使用 laravel 5.2 并且我的资产 url 有问题,我无法将 css 调用到或 js 链接

<script src="{{ URL::asset('/js/jquery.min.js') }}"></script>

or

或者

<script src="{{ asset('/js/jquery.min.js') }}"></script>

the url in html is: http://localhost/site/js/...js.

html 中的 url 是:http://localhost/site/js/...js

I have tried to add public but still not working

我试图添加 public 但仍然无法正常工作

got

得到了

NotFoundHttpException in RouteCollection.php line 161:

回答by idro2k

honestly, just try it like this,

老实说,就这样试试

<link href="/css/app.css" rel="stylesheet">

The / at the start of the address will asume you are at starting from the root of your server, which is the public directory.

地址开头的 / 将假定您从服务器的根目录开始,即公共目录。

回答by Chintan Hingrajia

Change /vendor/laravel/framework/src/Illuminate/Foundation/helpers.php/asset()function as follows:

更改/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php/asset()功能如下:

function asset($path, $secure = null)
{
   return app('url')->asset("public/".$path, $secure);
}

回答by Peyman

This is duplicate question. you can find my answer here also:

这是重复的问题。你也可以在这里找到我的答案:

Why I can't obtain the complete path of a CSS resource in a blade template?

为什么我无法在刀片模板中获取 CSS 资源的完整路径?

Do you see default laravel jsand cssfiles in resources folder? put your files there. I found this place the most convenient one.

你看到默认的laraveljscss文件了resources folder吗?把你的文件放在那里。我发现这个地方是最方便的。

You can load them in your blade like this: <link href="{!! asset('css/app.css') !!}" rel="stylesheet" type="text/css" >

您可以像这样将它们加载到刀片中: <link href="{!! asset('css/app.css') !!}" rel="stylesheet" type="text/css" >

if you are still facing problem try to run this on you project folder first and then try:

如果您仍然遇到问题,请尝试先在您的项目文件夹上运行它,然后尝试:

php artisan serve

php artisan serve

I hope it work for you. Any question just let me know.

我希望它对你有用。有任何问题请告诉我。

回答by Michel

Change <script src="{{ URL::asset('/js/jquery.min.js') }}"></script>

改变 <script src="{{ URL::asset('/js/jquery.min.js') }}"></script>

to

<script src="{{ URL::asset('js/jquery.min.js') }}"></script>

Notice the /before js, possibly the reason why your css and js links are not working. Do same on the css as well.

注意/之前的 js,可能是你的 css 和 js 链接不起作用的原因。在 css 上也做同样的事情。

回答by Leo

Do:

做:

  1. php artisan config: clear
  2. php artisan cache: clear
  3. delete public/css and public/js
  4. gulp
  5. try it again
  1. php artisan 配置:清除
  2. php artisan 缓存:清除
  3. 删除 public/css 和 public/js
  4. 吞咽
  5. 再试一次

you have to put your css, js, images etc in the public folder. The root of the asset method is the public folder so if you have a public/css/style.css file, you can get its path by using asset('css/style.css').

你必须把你的 css、js、图像等放在 public 文件夹中。资产方法的根是公共文件夹,所以如果你有一个 public/css/style.css 文件,你可以通过使用 asset('css/style.css') 来获取它的路径。

The asset folder is for storing less or sass files which have to be compiled into a css file.

资产文件夹用于存储必须编译成 css 文件的 less 或 sass 文件。