angular - 将 Angular 部署到 IIS 7.5 无法正常工作
问题描述
我,想部署我的 Angular 项目,但它丢失了组件
我的过程
步骤1:
我执行cmd:
ng-build --prod
第2步:
我在 IIS 中添加网站,路径在dist
第 3 步:
索引页面工作正常但未找到其他组件登录 404
http://localhost/login HTTP 错误 404.0 - 未找到
在文件App.Module.Ts我是配置路由
const routesConfig:Routes=
[
{path:"kinds",component:KindsComponent},
{path:"",component:FilmsComponent},
{path:"films",component:FilmsComponent},
{path:"login",component:LoginComponent},
{path:"accounts",component:AccountsComponent},
{path:"customers",component:CustomersComponent},
{path:"director",component:DirectorComponent}
];
再次注意: http://localhost工作正常,但在我构建项目并在 IIS 中部署时未找到其他组件
最后:
它如何工作?我的错误在哪里?
解决方案
您需要在 IIS 中为任何路由创建指向index.html的重定向,因为在 Angular SPA 中,应用程序负责路由而不是服务器,请参见此处Angular routing。
一旦请求例如http://yourdomain/ -> index.html得到服务,最后应用程序被引导。
但是,一旦请求例如http://yourdomain/login并且没有服务器端重定向到index.html
服务器,则没有任何资源映射到该路由并使用404 - Not Found
错误解析请求。
一旦您为任何以错误结尾的请求提供index.html404 - Not Found
(除了诸如 css、图像字体之类的资产),Angular 路由器就会接管并在应用程序启动后显示相应的视图。
这是 IIS 的示例重写规则:
<rule name="Angular" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
推荐阅读
- c++ - JSON返回的字符串如何转换成中文?使用 C 或 C++
- android - 当用户从浏览器下载文件时,我如何启动我的应用程序?
- android - 运行 adb 命令时出现错误 3221226356
- python - 如何设置 Python 代码库以使用另一个 Python 代码库并将其打包到生产环境中?
- java - 是否有一种解决方法可以在 CSS 中为 Region 的宽度和高度使用百分比值?
- jquery - 如何改变我的目标通过 CSS 或 jQuery 与另一个
- mongodb - 如何增强查询mongodb慢
- javascript - 使用 Object.create 实现继承中改变值的问题
- c - 如何比较C程序文件中的字符串结构数组
- python-3.x - 将函数结果解压缩到单独的列表中?