【问题标题】:Problem With Angular project's URL running in an Apache serverAngular 项目的 URL 在 Apache 服务器中运行的问题
【发布时间】:2020-06-05 05:32:07
【问题描述】:

看,我不知道我是否在标题中表达了自己,但就是这样。

我制作了一个 Angular 应用程序,客户可以通过我的网站购买健身房的计划。所以我做了一个路由方案,URL中的第一件事是健身房的ID(从我的数据库中恢复)然后是“/计划”。

例如:localhost:4200/000162/plans

问题来了,我构建了我的项目,文件夹结构类似于dist/__projectName__/__projectContent__

使用命令ng build --prod --base-href /vysor_vendas_online/app/

然后我把它所有的 projectContent 放在我的 Apache 服务器文件夹中的路径 www/vysor_vendas_online/app/ 中,并尝试在我的浏览器中运行。

localhost:8080/vysor_vendas_online/app/index.html?000162/plans 但什么也没有发生,它停留在白屏中,没有控制台错误,但是当我尝试使用 Angular 服务器localhost:4200/000162/plans 时它可以工作。

我在这里做错了什么?我应该如何访问我在代码中指定的路由?

【问题讨论】:

    标签: angular url routing


    【解决方案1】:

    正如官方deployment doc所说的

    路由应用必须回退到 index.html

    Apache:向 .htaccess 文件添加重写规则,如图所示 (https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/):

    RewriteEngine On
    # If an existing asset or directory is requested go to it as it is
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]
    
    # If the requested resource doesn't exist, use index.html
    RewriteRule ^ /index.html
    

    【讨论】:

    • 只有通过编辑我的 .htaccess 文件,我才能在 index.html? 之后传递参数??像这样localhost:8080/vysor_vendas_online/app/index.html?000162/plans
    • 参数和其他路由人员由角度路由器处理。但作为先决条件,您必须使用 .htaccess 配置 apache 以将您的请求回退到 angular 的 index.html。
    • 哦,好的,我会尝试并返回结果。非常感谢!!
    • 同样的事情 :(,我在 .htaccess 文件中添加了RewriteEngine On。然后尝试:localhost:8080/vysor_vendas_online/app/index.html?000162/planos 然后:无法匹配任何路由。URL 段:'index.html' - Angular 错误,然后我删除了“index.html”,如下所示:localhost:8080/vysor_vendas_online/app?000162/planos 仍然没有,我做错了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2011-12-19
    • 2013-09-21
    • 2011-10-01
    相关资源
    最近更新 更多