在本教程中,我们将看到如何 建立并托管 角度10应用程序。可以使用不同的配置来托管角度应用程序。
托管Angular 10应用程序的常规方法
开发之后,我们可以使用Angular CLI命令为产品版本创建angular应用程序的编译版本。
重击
ng build --prod
因为它将在内部创建应用程序的编译版本 dist 夹。为了托管应用程序,请将所有已编译的文件上传到您的服务器根目录,即 public_html
After that, you have to add a .htaccess
file into the same root directory
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
</IfModule>
在子文件夹中托管Angular
要将角度应用程序部署到根托管文件夹的子文件夹中,即 public_hmtl
Assume if you want to add your angular application into the services
folder.
示例网址: http://example.com/services
ng build --prod --base-href=/services/
这将在内部执行构建时设置应用程序基础 index.html 文件,您无需执行任何操作。
index.html
<base href="/services/">
对于此子文件夹,您必须在htaccess文件中定义子文件夹。
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /services/index.html [NC,L]
</IfModule>
注意: 上述方法仅适用于apache和nginx服务器,并且您的angular应用程序未启用SSR通用。
获取有关的更多教程 角度10