【问题标题】:Compiling Angular in production mode get errors Uncaught SyntaxError: Unexpected token '<'在生产模式下编译 Angular 得到错误 Uncaught SyntaxError: Unexpected token '<'
【发布时间】:2022-02-18 10:10:04
【问题描述】:

您好,我在我的 Angular 8 应用程序的控制台浏览器中收到此错误,我看到浏览器窗口为空白,我收到此错误

Uncaught SyntaxError: Unexpected token '<'

我尝试运行我的 ng 构建

ng build --base-href / --aot --prod
ng build --base-href /my_app/ --aot --prod
ng build --prod
ng build --aot --prod --output-hashing none

没有任何效果...

我的 apache 配置文件

<VirtualHost *:80>
        DocumentRoot "/var/www/html/MrpProy/my_app/dist/my_app/"
        ServerName mydomain.com
        <Directory /var/www/html/MrpProy/my_app/dist/my_app/>
            RewriteEngine on
            # Rewrite everything else to index.html to allow HTML5 state links
            RewriteRule ^ index.html [L]
        </Directory>
    </VirtualHost>

这是怎么解决的?

【问题讨论】:

  • 你的代码在哪里?您的模板中可能只有不匹配的大括号。
  • 你的意思是我的代码所在的路径?
  • 不,我的意思是在这里将您的代码显示为minimal reproducible example 可以让我们仔细检查它。不过,先检查一下自己,看看你是否有无与伦比的牙套。一个好的 IDE 会突出显示它可能发生的一般区域。
  • 您可能也在 javascript 请求中返回 index.html..
  • 是一个角度项目..当我编译项目工作正常并生成文件正常,但在浏览器中输入时出现错误

标签: javascript node.js angular apache angular-material


【解决方案1】:

正如@MikeOne 之前在 cmets 中所说...

您可能也在 javascript 请求中返回 index.html..

因为您的 Apache 配置...

# Rewrite everything else to index.html to allow HTML5 state links
RewriteRule ^ index.html [L]

这实际上将一切(不仅仅是“其他一切”)重写为index.html。因此,当请求anything.js 时,服务器以index.html 响应。所以,是的,浏览器正在尝试将 index.html 解析为 JavaScript。

您的 Apache 配置应该是这样的,以防止对静态资源的请求被重写为 index.html

RewriteEngine on

# Rewrite everything that does not look-like a file to index.html
RewriteRule !\.\w{2,4}$ index.html [L]

或者,效率稍低(但可能更灵活):

# Rewrite everything that is not a file to index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L]

【讨论】:

    猜你喜欢
    • 2015-04-21
    • 1970-01-01
    • 2016-10-01
    • 2017-05-30
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    相关资源
    最近更新 更多