Magento 2 不加载 CSS 和 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34040496/
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-08-23 15:50:24  来源:igfitidea点击:

Magento 2 not loading CSS and JavaScript

javascriptphpcssmagento2

提问by Scott

I have installed magento 2 successfully on localhost but I am not able to see admin panel as it render 404 error to me.

我已经在本地主机上成功安装了 magento 2,但我无法看到管理面板,因为它向我呈现 404 错误。

Secondly, When I open front-end then CSS and JavaScript is not loading.They also renders 404 errors.

其次,当我打开前端时,CSS 和 JavaScript 不会加载。它们也会呈现 404 错误。

Also When I try to run command:

另外当我尝试运行命令时:

{your Magento install dir}/bin/magento setup:static-content:deploy

I got the following error

我收到以下错误

[InvalidArgumentException]                                              
There are no commands defined in the "setup:static-content" namespace.

回答by Mohammed Anis

This One Worked for Me. use this Command php bin/magento setup:static-content:deploy

这个对我有用。使用这个命令php bin/magento setup:static-content:deploy

Step 1 : In CMD Open your root directory using cd command

第 1 步:在 CMD 中使用 cd 命令打开您的根目录

Step 2 : php bin/magento setup:static-content:deploy- run this Command

第 2 步:php bin/magento setup:static-content:deploy- 运行此命令

then,Check Your pub/staticfolder, CSS and Js files will be available there

然后,检查您的pub/static文件夹,CSS 和 Js 文件将在那里可用

Refer this

参考 这个

回答by Surendra Kumar Ahir

Here is the simplest solution if showing version name in css path

如果在 css 路径中显示版本名称,这是最简单的解决方案

like : pub/static/version323334/

像:pub/static/version323334/

then run this query in my sql

然后在我的 sql 中运行这个查询

 INSERT INTO core_config_data (path, value) 
VALUES ('dev/static/sign', 0) 
ON DUPLICATE KEY UPDATE value = 0;

after that clear the config cache

之后清除配置缓存

 bin/magento cache:clean config

you can also disable static file version form admin

您还可以禁用静态文件版本表单管理员

enter image description here

在此处输入图片说明

回答by SIBHI S

Try the same command as root user by adding sudo like below

通过添加如下 sudo 尝试与 root 用户相同的命令

sudo php bin/magento setup:static-content:deploy

回答by Agilox

Though it is old question, its answers could not help me to fix my issue what would generate same error message. I suggest the followings:

虽然这是个老问题,但它的答案无法帮助我解决会产生相同错误消息的问题。我建议如下:

  1. At first, you can find its underlying cause. In your magento2 directory, find magento error as follows:
  1. 首先,您可以找到其根本原因。在您的 magento2 目录中,找到 magento 错误如下:

tail var/log/system.log

尾部变量/日志/system.log

Or find if there is any error in php error log file

或者在php错误日志文件中查找是否有错误

If you find isolated error, fix it.

如果您发现孤立的错误,请修复它。

  1. If no error is found, do the followings. Remove generate folder by executing the following commands:
  1. 如果没有发现错误,请执行以下操作。通过执行以下命令删除生成文件夹:
sudo rm -rf pub/static
sudo rm -rf var/cache
sudo rm -rf var/composer_home
sudo rm -rf var/generation
sudo rm -rf var/page_cache
sudo rm -rf var/view_preprocessed
sudo rm -rf pub/static
sudo rm -rf var/cache
sudo rm -rf var/composer_home
sudo rm -rf var/generation
sudo rm -rf var/page_cache
sudo rm -rf var/view_preprocessed

After deleting them, you can re-create them by executing the following commands:

删除它们后,您可以通过执行以下命令重新创建它们:

sudo php -f bin/magento setup:static-content:deploy

回答by Elavarasan

We also faced this issue once, and sorted it out. For this you need to go directly in bindirectory and use that command. For example,

这个问题我们也遇到过一次,解决了。为此,您需要直接进入bin目录并使用该命令。例如,

php magento setup:static-content:deploy

Some times if you run this command outside of binor from any directory

有时,如果您在bin任何目录之外或从任何目录运行此命令

php bin/magento setup:static-content:deploy

then you will get error like (may be it is because of linux system),

然后你会得到类似的错误(可能是因为 linux 系统),

[InvalidArgumentException]
  There are no commands defined in the "setup:static-content" namespace.

Update:

更新:

If any *.xmlfile in our custom modules is not valid then the same error will come.

如果*.xml我们自定义模块中的任何文件无效,则会出现相同的错误。

回答by Mukesh Prajapati

Run the following commands in CLI interface of your Magento2 root folder:

在 Magento2 根文件夹的 CLI 界面中运行以下命令:

$ php bin/magento setup:static-content:deploy
$ php bin/magento indexer:reindex

Then delete var folder by this command at your root of magento2.

然后通过此命令在 magento2 的根目录中删除 var 文件夹。

$ rm -rf var/*

Then refresh your homepage and admin panel.

然后刷新您的主页和管理面板。

回答by Afroz Alam

I also had same issue and below step solves my issue:

我也有同样的问题,下面的步骤解决了我的问题:

Step 1: Navigate to directory where magento is installed.
Step 2: Run "sudo php bin/magento setup:static-content:deploy"

第 1 步:导航到 magento 的安装目录。
第 2 步:运行“sudo php bin/magento setup:static-content:deploy”

回答by VIPIN A ROY

Provide static deploy command like this:

提供这样的静态部署命令:

php bin/magento setup:static-content:deploy

Please set full permission for pub and var folders.

请为 pub 和 var 文件夹设置完全权限。

回答by Petter Kjelkenes

This error happens when you have not setup permission correctly. It can't see that the command actually exist.

当您没有正确设置权限时会发生此错误。它看不到该命令实际存在。

Try running:

尝试运行:

sudo find . -type d -exec chmod 770 {} \; && sudo find . -type f -exec chmod 660 {} \; && sudo chmod u+x bin/magento sudo chown -R $(whoami):www-data .

sudo find . -type d -exec chmod 770 {} \; && sudo find . -type f -exec chmod 660 {} \; && sudo chmod u+x bin/magento sudo chown -R $(whoami):www-data .

Change www-data to appropriate webserver user. e.g. apache or www-data.

将 www-data 更改为适当的网络服务器用户。例如 apache 或 www-data。

回答by Abhijith Sasikumar

This worked for me:

这对我有用:

1) Static content deploy. Run the below command from Magento 2 root directory:

1) 静态内容部署。从 Magento 2 根目录运行以下命令:

sudo php bin/magento setup:static-content:deploy

2) Clear everything in var/cachedirectory or flush the Magento 2 cache using the below command:

2)var/cache使用以下命令清除目录中的所有内容或刷新 Magento 2 缓存:

php bin/magento cache:flush

3) Set proper permissions for Magento 2 directories by executing the below command from Magento 2 root directory:

3) 通过从 Magento 2 根目录执行以下命令为 Magento 2 目录设置适当的权限:

sudo find . -type d -exec chmod 770 {} \; && sudo find . -type f -exec chmod 660 {} \; && sudo chmod u+x bin/magento

Hope this helps.

希望这可以帮助。