Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 路由器不';t使用命令ng build--prod处理angular4_Javascript_Angular - Fatal编程技术网

Javascript 路由器不';t使用命令ng build--prod处理angular4

Javascript 路由器不';t使用命令ng build--prod处理angular4,javascript,angular,Javascript,Angular,我试图通过跟随《英雄之旅》教程学习Angular(4.3.3版)。我从一个干净的项目开始,然后从那里开始 我已经实现了英雄之旅的路由系统。当我使用ng serve运行它时,它工作得很好,但是如果我使用ng build和选项--output path进行构建,并将内容输出到我的apache localhost服务器和节点http服务器,然后尝试运行它,那么它工作得很好,但是当我将它粘贴到地址栏中时,路由系统不工作 我使用的是干净的angular cli项目的所有默认配置 例如,如果我单击“仪表板”

我试图通过跟随《英雄之旅》教程学习Angular(4.3.3版)。我从一个干净的项目开始,然后从那里开始

我已经实现了英雄之旅的路由系统。当我使用
ng serve
运行它时,它工作得很好,但是如果我使用
ng build
和选项
--output path
进行构建,并将内容输出到我的apache localhost服务器和节点http服务器,然后尝试运行它,那么它工作得很好,但是当我将它粘贴到地址栏中时,路由系统不工作

我使用的是干净的angular cli项目的所有默认配置

例如,如果我单击“仪表板”链接,我可以转到“英雄”仪表板,但是,如果我在地址栏中粘贴或写入相同的链接,它将不起作用。它给我“404未找到”

我找不到问题。我是不是遗漏了什么

有人能指出问题出在哪里吗

我的完整ng生成命令:

ng build --prod --aot=false --extract-css=true  --output-path "F:\\www\\angular\\heroes" --bh "http://localhost/angular/heroes/" --deploy-url "http://localhost/angular/heroes/"

因为默认情况下,角度路由器使用。URL实际上不存在于服务器上,仅存在于用户浏览器中。这就是你得到404状态码的原因

为了能够直接访问URL(无需单击首页上的任何链接),您需要配置服务器(Apache、Nginx等)以将任何请求重定向到index.html

对于Apache,请使用重写规则:

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
对于Nginx,请尝试

try_files $uri $uri/ /index.html;

您可以在文档中找到所有这些(以及更多内容)。

当您使用Apache server时,您必须在根目录中使用.htaccess文件,并且必须在该.htaccess文件中添加一个。因为,根据:

如果应用程序使用Angular路由器,则必须将服务器配置为 请求文件时返回应用程序的主机页(index.html) 它没有


您可以查看详细信息。

尝试使用
HashLocationStrategy
将其添加到应用程序模块中的提供者数组中使用/dist文件夹或仅复制/dist的内容?实际上,我不需要复制内容或dist文件夹,因为我正在使用
ng build
命令的
--输出路径
选项