我有一个Angular 4单页面应用程序。当我刷新页面或在浏览器中输入有效的URL时,我会被重定向到应用程序的根路径。
我已将我的nginx配置更改为将所有请求重定向到/index.html,这修复了我在浏览器刷新时获取404的初始问题。
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,所以有没有办法在浏览器刷新或输入网址时让Angular为正确的页面提供服务?
答案 0 :(得分:1)
我猜[AuthGuard]会给你带来麻烦。