使用Angular 2和Apache的路由器不能在prod模式下工作
我在生产模式下开发angular4应用程序时遇到问题。我正在按照上指示的步骤进行操作。apache设置的目录是我的项目的文件夹dist。以下是我的项目的亮点: 应用程序模块.ts使用Angular 2和Apache的路由器不能在prod模式下工作,angular,apache,.htaccess,deployment,router,Angular,Apache,.htaccess,Deployment,Router,我在生产模式下开发angular4应用程序时遇到问题。我正在按照上指示的步骤进行操作。apache设置的目录是我的项目的文件夹dist。以下是我的项目的亮点: 应用程序模块.ts const appRoutes: Routes = [ { path: 'politica-privacidade', component: PoliticaPrivacidadeComponent }, { path: '', component: PaginaInicialCom
const appRoutes: Routes = [
{ path: 'politica-privacidade', component: PoliticaPrivacidadeComponent },
{ path: '', component: PaginaInicialComponent }
];
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
PaginaInicialModule,
PoliticaPrivacidadeModule,
RouterModule.forRoot(appRoutes)
],
providers: [],
bootstrap: [
AppComponent
]
})
export class AppModule { }
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
.htaccess
const appRoutes: Routes = [
{ path: 'politica-privacidade', component: PoliticaPrivacidadeComponent },
{ path: '', component: PaginaInicialComponent }
];
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
PaginaInicialModule,
PoliticaPrivacidadeModule,
RouterModule.forRoot(appRoutes)
],
providers: [],
bootstrap: [
AppComponent
]
})
export class AppModule { }
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
Apache配置文件
<VirtualHost 80.241.208.103:80>
ServerName coreografando.com.br
ServerAlias www.coreografando.com.br
ServerAdmin braulio@braulioti.com.br
DocumentRoot "/var/www/coreografando/dist"
ScriptAlias /cgi-bin/ "/var/www/coreografando/cgi-bin/"
<IfModule mod_suphp.c>
suPHP_UserGroup apache apache
suPHP_ConfigPath /var/www/coreografando/dist
</IfModule>
<Directory "/var/www/coreografando/dist">
</Directory>
</VirtualHost>
它可能与您如何构建应用程序无关,而是与您的服务器如何处理传入路由有关。我认为
ng serve
只需将所有请求传送到index.html,角度路由器就可以在那里获取请求的路径。您当前的apache服务器执行其他操作。在我看来,您的.htaccess文件工作不正常,尽管我对它不是很熟悉
angular seed项目有一个关于如何将angular构建部署到apache服务器的简短教程。下面是他们的.htaccess的外观:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
“路由器不能在产品模式下工作”是什么意思?它会抛出错误吗?它的行为是什么?在这个例子中/politica privacidade导致404,我想根路径确实起作用了,对吗?当您尝试通过子例程初始化页面(例如,通过在浏览器中刷新此路径)时,是否会导致404,或者即使通过angulars导航,您也无法调用此路由?完全正确。但在开发模式中,我对子例程并没有问题。我相信这是我在apache中进行部署或某些配置的方式,但在开发模式下,您没有使用apache服务器,对吗?谢谢您的帮助!问题不在于.htaccess文件的设置和conf文件中的yes。丢失了“AllowOverride All”。谢谢他们的指导啊,是的,我读过,但我真的不明白这意味着什么。很高兴它帮你解决了问题。我会更新我的答案以备将来参考