Angular 开发/Apache 而不是 ng 服务

Angular Development / Apache instead of ng serve

我正在尝试找到一种在我的开发笔记本电脑上使用 apache 而不是 "ng serve" 的方法。 Apache 运行 与 Symfony、ExtJs 的 VirtualHost 完美搭配。 我已经为我的 Angular 测试创建了一个特定的 VirtualHost,但我有一个白页

<VirtualHost *:80>
ServerAdmin webmaster@angulardemo.lan
DocumentRoot "/Users/maquejp/Development/angular/demo/src"
ServerName angulardemo.lan
ServerAlias www.angulardemo.lan
ErrorLog "/usr/local/var/log/httpd/angulardemo.lan-error_log"
CustomLog "/usr/local/var/log/httpd/angulardemo.lan-access_log" common

<Directory "/Users/maquejp/Development/angular/demo/src">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order Allow,Deny
    Allow from All
    Require all granted
</Directory>

另外我添加了一个.htaccess

    Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/api

# otherwise forward it to index.html 
RewriteRule ^.*$ - [NC,L]
RewriteRule ^app/. /app/index.html [NC,L]

当然我重启了apache (apachectl -k restart)

供参考,我在OSX下。

为避免错误响应:这不是部署在生产服务器上(ng build 无济于事)

谢谢!

我在远程 Linux 主机上使用 apache2 进行开发和部署。我的发展路径是~/DEV/ProjectName。在我的 package.json 文件中添加以下内容:

"scripts": {
    [snip],
    "watcher": "ng build --base-href=/ProjectName/dist/ProjectName/ --watch",
    [snip]
},

然后我开始构建过程 yarn watcher

顺便说一句,其中大部分内容都可以在 Angular 文档 here.

中找到

--watch 标志会导致构建过程在对项目文件进行更改时重新运行。 --base-href 构建 URL 以使用路径作为 index.html 文件的基础。

然后我将我的 Apache 配置 (/etc/apache2/conf-enabled/project-dirs.conf) 更新为如下所示(每个项目一个):

alias /ProjectName /home/homeDir/DEV/ProjectName
<Directory "/home/homeDir/DEV/ProjectName">
    options Indexes FollowSymlinks
    Allow from all
    Satisfy Any
</Directory>

我还添加了以下 .htaccess 文件来解决在我的项目中使用路由的问题。

<IfModule mod_rewrite.c>
    RewriteEngine on
# Don't rewrite files or directories
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
# Rewrite everything else to index.html
# to allow html5 state links
    RewriteRule ^ index.html [L]
</IfModule>

这还需要更新 projects\architect\build\options\assest 节中的 angular.json

"assets": [
    "src/assets/YourIcon.ico",
    "src/assets",
    "src/.htaccess"
]

这 "should" 导致 .htaccess 文件被放置在上面显示的构建目录中。

我还没有想出一个项目是如何使网页在构建运行时自动刷新。目前需要手动刷新。