Angular Development / Apache而不是ng serve

问题描述 投票:0回答:1

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

<VirtualHost *:80>
ServerAdmin [email protected]
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对您没有帮助)

谢谢!

angular apache virtualhost
1个回答
1
投票

我在远程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>

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

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

此“应该”将.htaccess文件放置在上面所示的生成目录中。

我还没有弄清的一个问题是如何在构建运行时使网页自动刷新。目前需要手动刷新。

© www.soinside.com 2019 - 2024. All rights reserved.