我有一个Angular 4单页面应用程序 . 当我刷新页面或在浏览器中输入有效的URL时,我会被重定向到应用程序的根路由 .
我已经更改了我的nginx配置,将所有请求重定向到/index.html,这修复了我在浏览器刷新时获取404s的初始问题 .
server {
listen 80;
server_name bla.com www.bla.com
location / {
root /home/admin/web/bla.com/public_html;
index index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 404 /index.html;
error_page 500 502 503 504 /50x.html;
# To allow POST on static pages
error_page 405 =200 $uri;
location = /50x.html {
root /usr/share/nginx/html;
}
}
编辑
角度路由简单地由RouterModule处理:
// ...
import { RouterModule, Routes } from '@angular/router';
// ...
const appRoutes: Routes = [
{ path: '', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'clients', component: ClientsComponent, canActivate: [AuthGuard], children: [
{ path: 'add', component: AddClientComponent, canActivate: [AuthGuard]}
]},
{ path: 'projects', component: ProjectsComponent, canActivate: [AuthGuard], children: [
{ path: 'add', component: AddProjectComponent, canActivate: [AuthGuard] }
]},
{ path: 'profile', component: ProfileComponent, canActivate: [AuthGuard] },
{ path: 'invoices', component: InvoicesComponent, canActivate: [AuthGuard], children: [
{ path: 'invoices/new-invoice', component: NewInvoiceComponent, canActivate: [AuthGuard] }
]}
];
我真的不想使用HashLocationStrategy,所以有没有办法在浏览器刷新或输入URL时让Angular提供正确的页面?
1 回答
我猜[AuthGuard]给你带来了麻烦 .