我在角9中有一个非常简单的路由模块。
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CalculatorsComponent } from './calculators/calculators.component';
const routes: Routes = [
{ path: 'calculators', component: CalculatorsComponent },
{ path: '', redirectTo: '/calculators', pathMatch: 'full' },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
我正在运行一个本地服务器,当我转到http://localhost:4200
时,它正确地将我重定向到http://localhost:4200/calculators
。但是,如果我试图通过在地址栏中输入http://localhost:4200/calculators
或在页面重定向后刷新它来直接访问它,我将得到一个404状态代码和Cannot GET /calculators
作为响应。
我觉得这只是一个页面应用程序的角度,但如果我错了,请纠正我。如果这是预期的,当用户试图导航到http://localhost:4200/calculators
时,我将如何处理我的网站。我希望他们能够访问该URL,而不必通过根视图导航到/calculators
视图。
发布于 2020-06-02 22:48:31
问题在于你如何运行这个有角度的应用程序。
如果它位于nginx或apache内部,则需要配置重写。
对于nginx
location / {
index index.html;
try_files $uri /index.html;
}
对于apache,需要在根dir中添加/更改.htaccess。
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html
https://stackoverflow.com/questions/62166734
复制