简体   繁体   English

域名下托管Angular 9 App

[英]Hosting Angular 9 App under a domain name

I have searched a lot for answers on hosting an angular app, I have a production build angular package which has folders like so:我已经搜索了很多关于托管 angular 应用程序的答案,我有一个生产版本 angular package,其中包含如下文件夹:

在此处输入图像描述

I happen have a basic hosting service with www.example.com as domain, I was able to upload into the public_html folder, and was able to use the angular app.我碰巧有一个以www.example.com作为域的基本托管服务,我能够上传到 public_html 文件夹,并且能够使用 angular 应用程序。 However the url has #, meaning even the home page has a url structure like wwww.example.com/#/home, is there any way where I can at least show the home page without the #/ some thing like wwww.example.com and it shows the homepage.然而,url 有 #,这意味着即使是主页也有一个 url 结构,如 wwww.example.com/#/home,有什么方法可以让我至少在没有 #/ 的情况下显示主页,比如 wwww.example。 com 并显示主页。

There are 2 things you need to consider here:这里有两件事你需要考虑:

  1. Make sure your angular application is using the HTML5 location strategy: https://angular.io/guide/router#locationstrategy-and-browser-url-styles确保您的 angular 应用程序正在使用 HTML5 定位策略: https://angular.io/guide/router#locationstrategy-and-browser-url-style

  2. Make sure your hosting environment supports Single Page Applications.确保您的托管环境支持单页应用程序。 That means that all paths that are not mapped to physical files/api endpoints or other resources should return the same index.html file since they will be handled on the client side by the angular application.这意味着所有未映射到物理文件/api 端点或其他资源的路径都应返回相同的index.html文件,因为它们将由 angular 应用程序在客户端处理。

You have switched from the default PathLocationStrategy to HashLocationStrategy .您已从默认PathLocationStrategy切换到HashLocationStrategy Revert this.恢复这个。

After you switched back to PathLocationStrategy , make sure, your server is set up for SPA serving;切换回PathLocationStrategy后,请确保您的服务器已设置为 SPA 服务; meaning all paths that do not point to existing resources, should give you your index.html .意思是所有不指向现有资源的路径,应该给你你的index.html

Also don't forget to specify <base href="/"> .也不要忘记指定<base href="/"> This will make sure your routing works when the app is booted in a nested path.当应用程序在嵌套路径中启动时,这将确保您的路由正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM